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.
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 :
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 :
Options / attributes available with Radiobutton:
SN | Option | Description |
---|---|---|
1 | activebackground | The background color of the widget when it has the focus. |
2 | activeforeground | The font color of the widget text when it has the focus. |
3 | anchor | It 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. |
4 | bg | The background color of the widget. |
5 | bitmap | It is used to display the graphics on the widget. It can be set to any graphical or image object. |
6 | bd | It represents the size of the border. |
7 | command | This option is set to the procedure which must be called every-time when the state of the radiobutton is changed. |
8 | cursor | The mouse pointer is changed to the specified cursor type. It can be set to the arrow, dot, etc. |
9 | font | It represents the font type of the widget text. |
10 | fg | The normal foreground color of the widget text. |
11 | height | The vertical dimension of the widget. It is specified as the number of lines (not pixel). |
12 | highlightcolor | It represents the color of the focus highlight when the widget has the focus. |
13 | highlightbackground | The color of the focus highlight when the widget is not having the focus. |
14 | image | It can be set to an image object if we want to display an image on the radiobutton instead the text. |
15 | justify | It represents the justification of the multi-line text. It can be set to CENTER(default), LEFT, or RIGHT. |
16 | padx | The horizontal padding of the widget. |
17 | pady | The vertical padding of the widget. |
18 | relief | The type of the border. The default value is FLAT. |
19 | selectcolor | The color of the radio button when it is selected. |
20 | selectimage | The image to be displayed on the radiobutton when it is selected. |
21 | state | It 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. |
22 | text | The text to be displayed on the radiobutton. |
23 | textvariable | It is of String type that represents the text displayed by the widget. |
24 | underline | The default value of this option is -1, however, we can set this option to the number of character which is to be underlined. |
25 | value | The value of each radiobutton is assigned to the control variable when it is turned on by the user. |
26 | variable | It is the control variable which is used to keep track of the user's choices. It is shared among all the radiobuttons. |
27 | width | The horizontal dimension of the widget. It is represented as the number of characters. |
28 | wraplength | We 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 :
SN | Method | Description |
---|---|---|
1 | deselect() | It is used to turn of the radiobutton. |
2 | flash() | It is used to flash the radiobutton between its active and normal colors few times. |
3 | invoke() | It is used to call any procedure associated when the state of a Radiobutton is changed. |
4 | select() | It is used to select the radiobutton. |
Other tkinter related posts are :
# -----------------------------------------------------------------------------------------------------------------------------------------------------#
No comments