How to determine which Radiobutton has been selected and then extract the value?
Question:
Assuming the following code has been written:
self.firstRadioButton = Radiobutton(self.__canvas, text="ONE", fg='white', bg=BACKGROUND_COLOR, variable=self.selectedONE, value=1)
self.secondRadioButton = Radiobutton(self.__canvas, text="TWO", fg='white', bg=BACKGROUND_COLOR, variable=self.selectedTWO, value=2)
I am trying to determine which radio button has been selected and then extract the value of whichever one the user picks. How do I do this?
Answers:
The way it works – is you do not check which radio buttons is selected, rather you bind a radio button to an event.
You have to bind the radio button to a function or event.
reference these articles:
http://www.java2s.com/Tutorial/Python/0360__Tkinker/BindvariabletoRadioButton.htm
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
Try something similar to:
self.secondRadioButton.bind('<ButtonRelease-1>', self.__function_name_that_should_run_when_radiobtn_selected)
The key is to make sure that both radiobuttons share the same variable. Then, to know which one is selected you simply need to get the value of the variable.
Here is an example:
import tkinter
# function that is called when you select a certain radio button
def selected():
print(var.get())
root = tkinter.Tk()
var = tkinter.StringVar() #used to get the 'value' property of a tkinter.Radiobutton
# Note that I added a command to each radio button and a different 'value'
# When you press a radio button, its corresponding 'command' is called.
# In this case, I am linking both radio buttons to the same command: 'selected'
rb1 = tkinter.Radiobutton(text='Radio Button 1', variable=var,
value="Radio 1", command=selected)
rb1.pack()
rb2 = tkinter.Radiobutton(text='Radio Button 2', variable=var,
value="Radio 2", command=selected)
rb2.pack()
root.mainloop()
Assuming the following code has been written:
self.firstRadioButton = Radiobutton(self.__canvas, text="ONE", fg='white', bg=BACKGROUND_COLOR, variable=self.selectedONE, value=1)
self.secondRadioButton = Radiobutton(self.__canvas, text="TWO", fg='white', bg=BACKGROUND_COLOR, variable=self.selectedTWO, value=2)
I am trying to determine which radio button has been selected and then extract the value of whichever one the user picks. How do I do this?
The way it works – is you do not check which radio buttons is selected, rather you bind a radio button to an event.
You have to bind the radio button to a function or event.
reference these articles:
http://www.java2s.com/Tutorial/Python/0360__Tkinker/BindvariabletoRadioButton.htm
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
Try something similar to:
self.secondRadioButton.bind('<ButtonRelease-1>', self.__function_name_that_should_run_when_radiobtn_selected)
The key is to make sure that both radiobuttons share the same variable. Then, to know which one is selected you simply need to get the value of the variable.
Here is an example:
import tkinter
# function that is called when you select a certain radio button
def selected():
print(var.get())
root = tkinter.Tk()
var = tkinter.StringVar() #used to get the 'value' property of a tkinter.Radiobutton
# Note that I added a command to each radio button and a different 'value'
# When you press a radio button, its corresponding 'command' is called.
# In this case, I am linking both radio buttons to the same command: 'selected'
rb1 = tkinter.Radiobutton(text='Radio Button 1', variable=var,
value="Radio 1", command=selected)
rb1.pack()
rb2 = tkinter.Radiobutton(text='Radio Button 2', variable=var,
value="Radio 2", command=selected)
rb2.pack()
root.mainloop()