Reputation: 2507
I am trying to print not just the Value of the radio button but also it's Name. so that I can do this "You selected 'Radio 2' with Value of '23' "
from tkinter import *
class Gui:
def __init__(self, master):
self.master = master
self.var = StringVar()
self.var.set("Cat")
radio1 = Radiobutton(self.master, variable=self.var, text="radio1", value="Cat")
radio1.bind('<Double-1>', lambda :self.show_radioname("radio1"))
radio1.pack()
radio2 = Radiobutton(self.master, variable=self.var, text="radio2", value="Dog")
radio2.bind('<Double-1>', lambda :self.show_radioname("radio2"))
radio2.pack()
radio3 = Radiobutton(self.master, variable=self.var, text="radio3", value="Horse")
radio3.bind('<Double-1>', lambda :self.show_radioname("radio3"))
radio3.pack()
submit_button = Button(self.master, text="print", command=self.show_var)
submit_button.pack()
def show_var(self):
print(self.var.get())
@staticmethod
def show_radioname(radio_name):
print(radio_name)
I can get the Value of The Radio But could not get their Text Name
Upvotes: 0
Views: 1307
Reputation: 4407
When you create a binding with bind
, Tkinter automatically adds an argument that has information about the event. You must take care to account for this extra argument. (Refer to this answer)
So, if you are using lambda
, you can just add a parameter to it, like this :
lambda event: self.show_radioname("radio1"))
from tkinter import *
class Gui:
def __init__(self, master):
self.master = master
self.var = StringVar()
self.var.set("Cat")
radio1 = Radiobutton(self.master, variable=self.var, text="radio1", value="Cat")
radio1.bind('<Double-1>', lambda event: self.show_radioname("radio1"))
radio1.pack()
radio2 = Radiobutton(self.master, variable=self.var, text="radio2", value="Dog")
radio2.bind('<Double-1>', lambda event: self.show_radioname("radio2"))
radio2.pack()
radio3 = Radiobutton(self.master, variable=self.var, text="radio3", value="Horse")
radio3.bind('<Double-1>', lambda event: self.show_radioname("radio3"))
radio3.pack()
submit_button = Button(self.master, text="print", command=self.show_var)
submit_button.pack()
self.master.mainloop()
def show_var(self):
print(self.var.get())
@staticmethod
def show_radioname(radio_name):
print(radio_name)
Gui(Tk())
Upvotes: 1