Python tkinter Radiobutton

Displaying three radiobuttons
Basics of Python Tkinter
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500")  # Size of the window 

r1_v = tk.IntVar()

r1 = tk.Radiobutton(my_w, text='Passed', variable=r1_v, value=1)
r1.grid(row=1,column=1) 

r2 = tk.Radiobutton(my_w, text='Failed', variable=r1_v, value=0)
r2.grid(row=1,column=2) 

r3 = tk.Radiobutton(my_w, text='Appearing', variable=r1_v, value=5)
r3.grid(row=1,column=3) 

my_w.mainloop()  # Keep the window open

Key attributes about Radiobutton

In above code we have linked the variable 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 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.

Events of radiobutton

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()
Difference between checkbox and radio buttons in our Checkbutton tutorial

  1. Exercise on radiobutton
  2. 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.
Solution


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer