Reputation: 411
Hi I am having trouble calling functions in tkinter
Here is the code
class Demo(tk.Frame):
def ShowOption(self):
print(self.v1.get())
def __init__(self,controller):
tk.Frame.__init__(self,width=800, height=600)
f = Frame(self)
optionList = ('A', 'B','C')
self.v1 = tk.StringVar()
self.v1.set(optionList[0])
Opt1 = tk.OptionMenu(f, self.v1, *optionList,command = self.ShowOption)
Opt1.grid(row=2,column=2,sticky='w')
f.place(relx = 0.5,rely=0.5,anchor='c')
The problem I have is if I use this method it states the function takes 1 postional argument and none were given but if I use
Opt1 = tk.OptionMenu(f, self.v1, *optionList,command = self.ShowOption() )
The function runs straight away when the class is created.
Any help greatly appreciated.
Upvotes: 0
Views: 45
Reputation: 47193
Callback for the command
option of OptionMenu
expects an argument which is the selected item.
So either you use lambda
to skip the argument:
command=lambda v: self.ShowOption()
Or redefine ShowOption()
to accept an argument:
def ShowOption(self, value):
...
Upvotes: 1