简体   繁体   中英

Python… Hide widget upon menu option selection - Tkinter

Working with Tkinter, I am trying to make a label disappear and another appear in its place when a specific option is selected with MenuOption(). Can I accomplish this without the need of a "refresh" button?

updated with a sample of my code:

mGui = Tk()
mGui.geometry('570x130+700+200')
mGui.resizable(width = FALSE, height = FALSE)
mGui.title('Title')


mylist = ['henry', 'tom', 'phil']
someValue = StringVar()

mLabel = Label(text = 'name:  ').grid(row = 0, column = 0, sticky = E)

someMenu = OptionMenu(mGui, someValue, *mylist)
someMenu.grid(row = 0, column = 1, sticky = W)
someMenu.config(width = 14, anchor = W)

mGui.mainloop()

So, if someMenu.get() == 'tom' i want to hide mLabel...

so i've added the following:

def something():
        print someValue.get()

mylist = ['henry', 'tom', 'phil']
someValue = StringVar()
someValue.trace('w', something)

and am getting TypeError: 'NoneType' object is not callable.. hmmmmm

You can put a trace on someValue , which can call a function whenever the value changes. In that function you can do anything you want, including removing widgets.

This website has an example: http://effbot.org/tkinterbook/variable.htm

if someMenu.get == "tom":
    buttonName.pack()
else:
    buttonName.pack_forget()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM