Python Tkinter - radio button

Pybeginner
By -
3 minute read
0



We can also add a radio button into Tkinter. To add radio button, just use the RadioButton class like this:

rad = Radiobutton(window, text = 'first', value = 1)

 

Note that you must set the value for each radio button to a different value; otherwise, they will not work.

Therefore, the complete code will look like this

 

from tkinter import *

window = Tk ()
window.title ("First radio button")
window.geometry ('250x250')

rad1 = Radiobutton (window, text = 'first', value = 1 )
rad1.grid (column = 0, row = 0, padx = 15, pady = 15)

rad2 = Radiobutton (window, text = 'second', value = 2)
rad2.grid (column = 0, row = 1, padx = 15, pady = 15)

rad3 = Radiobutton (window, text = 'third', value = 3)
rad3.grid (column = 0, row = 2, padx = 15, pady = 15)

window.mainloop ()

 



 

Python tkinter - Events RadioButton

 

We can use 3 types of variables when passing an event to the check button which are :

IntVar()
StringVar()
BooleanVar()

 

Python tkinter - Capture of RadioButton's click event using IntVar ()

 

Here we will create a function that will print the value contained in the variable selected name that will contain IntVar (), and each time we select one of the verification buttons, the function will be executed along with the value that will be passed to the value attribute within each option button.

Attention: IntVar() accepts only integer values, otherwise it will present an error.

Therefore, the complete code will look like this:

 

from tkinter import *

window = Tk()
window.title("First radio button")
window.geometry('250x250')

def hello():
result = selected.get()
label.configure(text=result)
print('Option button value:', result)

label = Label(window, text="text")
label.grid(row=0,column=0, padx=10, pady=10)

selected = IntVar()
rad1 = Radiobutton(window, text = 'First', value = 1, variable = selected, command = hello)
rad1.grid(column = 0, row = 1, padx = 10 , pady = 15)

rad2 = Radiobutton(window, text = 'second', value = 2, variable = selected, command = hello)
rad2.grid(column = 1, row = 1, padx = 10, pady = 15)

rad3 = Radiobutton(window, text = 'Third party', value = 3, variable = selected, command = hello)
rad3.grid column = 2, row = 1, padx = 10, pady = 15)

window.mainloop()

 




Python tkinter - Capture RadioButton event using StringVar ()

 

Here we will do the same procedure, only instead of IntVar (), we will use StringVar ().

Attention: StringVar () accepts any value, but these values will be considered as strings.

Therefore, the complete code will look like this:

 

from tkinter import *

window = Tk()
window.title ("First radio button")
window.geometry ('250x250')

def hello():
result = selected.get()
label.configure(text=result)
print('Option button value:', result)

label = Label(window, text="text")
label.grid(row=0,column=0, padx=10, pady=10)

selected = StringVar()

rad1 = Radiobutton(window, text = 'First', value = 'ProgrammingRoad in Angola', variable = selected, command = hello)
rad1.grid(column = 0, row = 1, padx = 10, pady = 15)

rad2 = Radiobutton(window, text = 'second', value = 'ProgrammingRoad in India', variable = selected, command = hello)
rad2.grid(column = 1, row = 1 , padx = 10, pady = 15)

rad3 = Radiobutton(window, text = 'Third party', value = 'ProgrammingRoad in Brazil', variable = selected, command = hello)
rad3.grid(column = 2, row = 1, padx = 10, pady = 15)

window.mainloop()

 



 

Python Tkinter - RadioButton event capture using BooleanVar()

 

Here BooleanVar (), accepts only True or False (True or False), (0 or 1), otherwise it will present an error .

 

from tkinter import *

window = Tk ()
window.title ("First radio button")
window.geometry ('250x250')

def hello():
result = selected.get()
label.configure(text=result)
print('Option button value:', result)

label = Label(window, text="text")
label.grid(row=0,column=0, padx=10, pady=10)

selected = BooleanVar()
rad1 = Radiobutton(window, text = 'First', value = 1, variable = selected, command = hello)
rad1.grid(column = 0, row = 1)
rad2 = Radiobutton(window , text = 'second', value = 0, variable = selected, command = hello)
rad2.grid(column = 1, row = 1)

window.mainloop ()

 




Post a Comment

0Comments

Post a Comment (0)