Radiobuttons to select one from many

We use radiobuttons to make one choice from a selection of choices. Each button in the set is linked to the same variable. As one button is left-clicked with the mouse, the value associated with that particular button gets assigned as the value of the variable.

Radiobuttons to select one from many

How to do it...

Execute the program shown in the usual way.

# radiobuttons_1.py
#>>>>>>>>>>>>>>>
from Tkinter import *
root = Tk( )
root.title("Radiobuttons")
var_1 = StringVar( )
rad_1 = Radiobutton(root, text='violent', variable = var_1,  value="action").grid(row=0, column=0)
rad_2 = Radiobutton(root, text='love', variable = var_1,  value="romance").grid(row=0, column=1)
rad_2 = Radiobutton(root, text='conflict', variable = var_1,  value="war").grid(row=0, column=2)
def callback_1():
v_1 = var_1.get()
print v_1
button_1= Button(root, command=callback_1).grid(row=4, column=0)
root.mainloop()

How it works...

We have specified a special Tkinter string variable that we name as var_1. We can assign one of three possible string values depending on which radio button is clicked. A normal button is used to display the value var_1 has at any time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.11.247