Piiixxxel
Piiixxxel

Reputation: 23

Tkinter detect when OptionMenu selection has changed

Currently working on a Notepad application and having problems with the OptionMenu widget.

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

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

the openFileCurrentDir() function is never ran after changing the selection

Upvotes: 1

Views: 51

Answers (1)

Henry Yik
Henry Yik

Reputation: 22503

You can use the trace method on your 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())

Upvotes: 2

Related Questions