Tkinter检测OptionMenu选择何时更改

时间:2019-03-23 09:19:14

标签: python-3.x tkinter

当前正在使用记事本应用程序,并且OptionMenu小部件存在问题。

self.fileOptionMenu = OptionMenu(self.master, self.var, *self.fileList, command=self.openFileCurrentDir)
self.fileOptionMenu.pack()

def openFileCurrentDir(self):
    print("inside openFileCurrentDir()")

更改选择后永远不会运行openFileCurrentDir()函数

1 个答案:

答案 0 :(得分:2)

您可以在trace上使用StringVar方法。

def __init__(self,master):
    self.master = master
    self.var = StringVar()
    self.var.trace("w", self.openFileCurrentDir)
    l = ["A","B","C","D"]
    self.fileOptionMenu = OptionMenu(self.master, self.var, *l)
    self.fileOptionMenu.pack()

def openFileCurrentDir(self,*args):
    print("inside openFileCurrentDir()"+self.var.get())