python - Check what tkinter OptionMenu item was selected -


how check if user has selected "other" hopoptions selection, , enable otherentry if did? , disable again if select 1 of other options.

    class interface():         def __init__(self, window):             frame = frame(window)             frame.pack()              self.hoplabel = label(frame, text="hop:", anchor=e)             self.hoplabel.grid(row=0, column=0, sticky=ew)              hops = range(0,6)             hops.append("other")              self.selectedhop = stringvar(frame)             self.selectedhop.set(hops[0])             self.hopoptions = optionmenu(frame, self.selectedhop, *hops)             self.hopoptions.grid(row=0, column=2, sticky=ew)              self.otherentry = entry(frame, state=disabled)             self.otherentry.grid(row=0, column=1, sticky=ew)      root = tk()      app = interface(root)      root.mainloop() 

bind option menu command , add method class. command run class method value argument anytime option changed in menu. there can validation update otherentry widget. advise not doing from tkinter import * appears that's you've done. importing entire package have conflicts namespace. should suit needs:

from tkinter import *  class interface():         def __init__(self, window):             frame = frame(window)             frame.pack()              self.hoplabel = label(frame, text="hop:", anchor=e)             self.hoplabel.grid(row=0, column=0, sticky=ew)              hops = range(0,6)             hops.append("other")              self.selectedhop = stringvar(frame)             self.selectedhop.set(hops[0])             self.hopoptions = optionmenu(frame, self.selectedhop, *hops, command=self.optupdate)             self.hopoptions.grid(row=0, column=2, sticky=ew)              self.otherentry = entry(frame, state=disabled)             self.otherentry.grid(row=0, column=1, sticky=ew)          def optupdate(self, value):             if value == "other":                 self.otherentry.config(state=normal)             else:                 self.otherentry.config(state=disabled)  if __name__ == "__main__":     root = tk()     app = interface(root)     root.mainloop() 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -