嗨,我在 tkinter 中调用函数时遇到问题
这是代码
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')
我遇到的问题是,如果我使用这种方法,它会声明该函数需要 1 个位置参数并且没有给出,但是如果我使用
Opt1 = tk.OptionMenu(f, self.v1, *optionList,command = self.ShowOption() )
该函数在创建类时立即运行。
非常感谢任何帮助。