Tkinter: How to call a function using the OptionMenu widget?
from Tkinter import *root = Tk()a = StringVar()a.set("default")oc = StringVar(root)oc.set("Select")def function(x): if x == "yes": a.set("hello") print a.get() else: a.set("bye") print a.get()o = OptionMenu(root, oc, "yes", "no", command=function)o.pack()z = a.get() print zroot.mainloop()
OptionMenu executes function
now when the OptionMenu selection is changed -- if the option menu reads "yes", function
executes with "yes" as its parameter and sets a
to "hello" and sets a
to "bye" for all other options.