Header Ads

Header ADS

Radiobutton and its indicatoron attribute in tkinter


Tkinter : Radiobutton and its attributes

A radio button , sometimes also called as option button, allows the user to select or choose exactly one of the predefined set of options .

a radiobutton is used when the user have to select only one value from a set of values .

Radio buttons are named after the physical buttons used on old radios to select wave bands or preset radio stations. If such a button was pressed, other buttons would pop out, leaving the pressed button the only pushed in button.

Each group of Radio button widgets has to be associated with the same variable. Pushing a button changes the value of this variable to a predefined certain value.

for example : When you fill a form there is place where you select your gender and you can select only one option at that time it can be male or female or others .

 This is actually a radio button looks like ...


Syntax : 

rd = Radiobutton(master,options)

So let's code :

# radio-button : dynamiccoding.tech
from tkinter import *

def func():
	if var.get() == 1:
		print("radiobutton 1 is selected")
	elif var.get() == 2:
		print("radiobutton 2 is selected")
	elif var.get() == 3:
		print("radiobutton 3 is selected")
	else:
		print("please select radiobutton")

root = Tk()
root.title("Dynamiccoding.tech")
root.configure(bg='pink')

var = IntVar()
radio_1 = Radiobutton(root ,text = 'Radio 1',value = 1 , variable = var)
radio_1.pack(pady=10)

radio_2 = Radiobutton(root ,text = 'Radio 2',value = 2 , variable = var)
radio_2.pack(pady=10)

radio_3 = Radiobutton(root ,text = 'Radio 3',value = 3 , variable = var)
radio_3.pack(pady=10)


Button(root,text = "Press me ",command=func).pack(pady=5)

root.mainloop()

Output : 

As you can see in the code each radiobutton have a unique value which helps to find which radio button is selected and instead of creating a button you can also pass the command attribute directly in radiobutton .
if the radio button is selected the its value is stored in a variable " var " which is a IntVar( )  variable , you can also use other fromats like StringVar( ) or BooleanVar() etc .












Now instead of having radiobutton with circular holes containing white space , we can have radiobutton with complete text in a box.
So how can we do it .
for this we can use indicatoron ( stands for " Indicator on ' ) and place this option equal  to 0 .

So let's code :

 
# radio-button : dynamiccoding.tech
from tkinter import *

def func():
	if var.get() == 1:
		print("radiobutton 1 is selected")
	elif var.get() == 2:
		print("radiobutton 2 is selected")
	elif var.get() == 3:
		print("radiobutton 3 is selected")
	else:
		print("please select radiobutton")

root = Tk()
root.title("Dynamiccoding.tech")
root.configure(bg='pink')

var = IntVar()
radio_1 = Radiobutton(root ,text = 'Radio 1',value = 1 , variable = var,indicatoron=0)
radio_1.pack(pady=10)

radio_2 = Radiobutton(root ,text = 'Radio 2',value = 2 , variable = var,indicatoron=0)
radio_2.pack(pady=10)

radio_3 = Radiobutton(root ,text = 'Radio 3',value = 3 , variable = var,indicatoron=0)
radio_3.pack(pady=10)


Button(root,text = "Press me ",command=func).pack(pady=5)

root.mainloop()

Output : 

As you can see the small holes are now wipeout or removed from the radiobutton and now radiobutton will work like a toggle button .










Options / attributes available with Radiobutton:

SNOptionDescription
1activebackgroundThe background color of the widget when it has the focus.
2activeforegroundThe font color of the widget text when it has the focus.
3anchorIt represents the exact position of the text within the widget
 if the widget contains more space than the requirement of the text.
 The default value is CENTER.
4bgThe background color of the widget.
5bitmapIt is used to display the graphics on the widget.
It can be set to any graphical or image object.
6bdIt represents the size of the border.
7commandThis option is set to the procedure which must be called
 every-time when the state of the radiobutton is changed.
8cursorThe mouse pointer is changed to the specified cursor type.
 It can be set to the arrow, dot, etc.
9fontIt represents the font type of the widget text.
10fgThe normal foreground color of the widget text.
11heightThe vertical dimension of the widget. It is specified as the
 number of lines (not pixel).
12highlightcolorIt represents the color of the focus highlight when the widget
has the focus.
13highlightbackgroundThe color of the focus highlight when the widget is not having
the focus.
14imageIt can be set to an image object if we want to display an
image on the radiobutton instead the text.
15justifyIt represents the justification of the multi-line text.
It can be set to CENTER(default), LEFT, or RIGHT.
16padxThe horizontal padding of the widget.
17padyThe vertical padding of the widget.
18reliefThe type of the border. The default value is FLAT.
19selectcolorThe color of the radio button when it is selected.
20selectimageThe image to be displayed on the radiobutton when it is
selected.
21stateIt represents the state of the radio button. The default state
 of the Radiobutton is NORMAL. However, we can set this to
 DISABLED to make the radiobutton unresponsive.
22textThe text to be displayed on the radiobutton.
23textvariableIt is of String type that represents the text displayed by the
 widget.
24underlineThe default value of this option is -1, however, we can set
this option to the number of character which is to be
 underlined.
25valueThe value of each radiobutton is assigned to the control
 variable when it is turned on by the user.
26variableIt is the control variable which is used to keep track of the
user's choices. It is shared among all the radiobuttons.
27widthThe horizontal dimension of the widget. It is represented as
the number of characters.
28wraplengthWe can wrap the text to the number of lines by setting this
 option to the desired number so that each line contains only
 that number of characters.


Methods :
SNMethodDescription
1deselect()It is used to turn of the radiobutton.
2flash()It is used to flash the radiobutton between its active and normal colors
 few times.
3invoke()It is used to call any procedure associated when the state of a Radiobutton
 is changed.
4select()It is used to select the radiobutton.





# -----------------------------------------------------------------------------------------------------------------------------------------------------#

This blog is contributed by Yogesh singh .
Please leave a feedback in comment section

 

No comments

Powered by Blogger.