In above code we have linked the variable IntVar()r1_v to the radiobutton. This variable is common to all three radio buttons. ( In case of Checkbuttons the associated variables are different).
The value option is different for all the three radio buttons. This value will be associated to variable r1_v once the radio button is selected.
At any point of time maximum one radio button can be selected. ( We can select more than one Checkbuttons )
Collecting and Setting the values of Radiobutton 🔝
How to read the status ( value of the associated variable ) of the radio button.
my_val=r1_v1.get() #r1_v1 is the variable connected to radiobutton
How to set the value of the radiobutton ( to select or Unselect )?
r1_v1.set(1)
We will use the above concepts. Note that radiobutton can have values 1 or 0 or any other integer( integer variable ), Yes or No or any other string ( string variable) , True or False ( Boolean variable ). We will learn all three types here.
Capturing click event of checkbox and display the value of the variable
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
print('Radiobutton value :',r1_v.get())
r1_v = tk.IntVar() # We used integer variable here
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value=1,command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value=0,command=my_upd)
r2.grid(row=1,column=2)
r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v, value=5,command=my_upd )
r3.grid(row=1,column=3)
my_w.mainloop() # Keep the window open
Capturing click event of Radiobutton and display the string variable
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
print('Radiobutton value :',r1_v.get())
r1_v = tk.StringVar() # We used string variable here
r1_v.set('Passed') # Can assign value Appear or Failed
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value='Passed',command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value='Failed',command=my_upd)
r2.grid(row=1,column=2)
r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v, value='Appear',command=my_upd )
r3.grid(row=1,column=3)
my_w.mainloop() # Keep the window open
Now let us use one Boolean variable
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
print('Radiobutton value :',r1_v.get())
r1_v = tk.BooleanVar() # We assigned Boolean variable here
r1_v.set(True) # Can assign False
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value=True,command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value=False,command=my_upd)
r2.grid(row=1,column=2)
my_w.mainloop() # Keep the window open
How to set a default value ( select or unselect ) to a radiobutton 🔝
Check the code written above (for click event of Radiobutton and display the string variable) , we used one line to set the value of variable r1_v like this.
r1_v.set('Passed') # Can assign value Appear or Failed
We can change above code to set the radio button to a different selection.
Using all above knowledge let us try to make second group of radio buttons check or uncheck based on updating of first group of radiobuttons. Here we need to read the data of first radiobutton and then copy the same to second radiobutton.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
r2_v.set(r1_v.get()) # read from first group and set the second group
print('Radiobutton value :',r1_v.get())
r1_v = tk.StringVar()
r1_v.set('Passed') # (default value ) Can assign value Appear or Failed
r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value='Passed',command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value='Failed',command=my_upd)
r2.grid(row=1,column=2)
r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v, value='Appear',command=my_upd )
r3.grid(row=1,column=3)
r2_v = tk.StringVar()
r2_v.set('Passed') # default value
r4 = tk.Radiobutton(my_w, text='Passed', variable=r2_v, value='Passed',command=my_upd)
r4.grid(row=2,column=1)
r5 = tk.Radiobutton(my_w, text='Failed', variable=r2_v, value='Failed',command=my_upd)
r5.grid(row=2,column=2)
r6 = tk.Radiobutton(my_w, text='Appearing', variable=r2_v, value='Appear',command=my_upd )
r6.grid(row=2,column=3)
my_w.mainloop()
deselect() : DeSelect or off the radio button flash() : flash the radio button but leave it in its original state. invoke() : Select the radio button and call the function. select() : Select the radio button.
Here is an example where each button is connected to one method of our first Radio button r1.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x250") # Size of the window
my_w.title("www.plus2net.com") # Window title
font1=['Arial',15,'normal'] # Font type,size, style
def my_upd():
print('Radiobutton value :',r1_v.get())
r1_v=tk.StringVar(value='MySQL')
r1=tk.Radiobutton(my_w,text='PHP',variable=r1_v,value='PHP',command=my_upd,font=font1)
r1.grid(row=1,column=1,padx=20,pady=30)
r2=tk.Radiobutton(my_w,text='MySQL',variable=r1_v,value='MySQL',command=my_upd)
r2.grid(row=1,column=2,padx=20,pady=30)
r3=tk.Radiobutton(my_w,text='Python',variable=r1_v,value='Python',command=my_upd)
r3.grid(row=1,column=3,padx=20,pady=30)
bt1=tk.Button(my_w,text='select()',command=lambda:r1.select())
bt1.grid(row=2,column=1)
bt2=tk.Button(my_w,text='deselect()',command=lambda:r1.deselect())
bt2.grid(row=2,column=2)
bt3=tk.Button(my_w,text='flash()',command=lambda:r1.flash())
bt3.grid(row=2,column=3)
bt4=tk.Button(my_w,text='invoke()',command=lambda:r1.invoke())
bt4.grid(row=2,column=4)
my_w.mainloop() # Keep the window open
Use the indicatoron attribute in Tkinter Radiobuttons to toggle between traditional radio buttons and pushbutton style.
indicatoron can be 0 or 1 ( True or False ), default value is True.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x150") # Size of the window
def selected():
print("Selected value:", my_var.get())
my_w.title("Radiobutton Customization")
my_font=['Arial',18,'normal']
# Variable to hold the selected Radiobutton's value
my_var = tk.IntVar()
# List of options
options = [("PHP ", 1), ("Python ", 2), ("JSP ", 3)]
i=0
for (text, value) in options:
rd1=tk.Radiobutton(my_w, text=text, variable=my_var, value=value,
command=selected, fg="blue", bg="light gray",font=my_font,
selectcolor="yellow", activebackground="light blue",
activeforeground="yellow", indicatoron=0, width=10,
compound="left")
rd1.grid(row=0,column=i,pady=30 ,padx=8)
i=i+1
my_w.mainloop()
Create one columns of radio buttons representing languages and second column showing same languages with checkboxes. By Selecting the radio button the matching checkbox should select ( check ) , your change in selection of radio buttons should deselect previous one and check the new one
Example : If you select PHP Radio button then PHP checkbox should select. If you change your selection to Python then Python checkbox should select and at the same time PHP should deselect.
How do you create a Radiobutton in Tkinter?
Explain the basic syntax to create a Radiobutton widget in a Tkinter application, mentioning any required parameters.
What is the significance of the variable option in a Tkinter Radiobutton?
Discuss how the variable option is used to associate a Python variable with a group of Radiobuttons.
How do you group Radiobuttons in Tkinter?
Describe the method to group multiple Radiobuttons together, so selecting one option automatically deselects the others in the group.
Can you explain how to retrieve the value of a selected Radiobutton in Tkinter?
Provide an example to demonstrate retrieving the value from a selected Radiobutton using a Python variable.
What is the purpose of the value option in a Tkinter Radiobutton?
Explain how the value option is utilized to assign a unique identifier to each Radiobutton in a group.
How do you change the appearance of Radiobuttons in Tkinter?
Discuss the options available to customize the appearance of Radiobuttons, such as changing the color, size, or adding a text label.
How does the command option work in Tkinter Radiobuttons?
Explain the use of the command option to execute a function whenever a Radiobutton is selected.