0

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

3
  • grid... I've searched around and found the grid_remove method, just not sure how to integrate it upon selection of an option.. Commented Feb 6, 2014 at 0:45
  • Are you just wanting to change the text, or completely remove the label? Commented Feb 6, 2014 at 1:03
  • either or, prob more efficient to change the text but I haven't quite gotten that far yet (beginner programmer) Commented Feb 6, 2014 at 1:05

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Turns out I was missing *args within something(). Now it works! :)
1
if someMenu.get == "tom":
    buttonName.pack()
else:
    buttonName.pack_forget()

4 Comments

Please also briefly explain the code to be more educative.
This may not work -- depending on how the widgets were originally layed out, the next time they are added, they may get added in a different place.
@BryanOakley Agreed. It was more less of a tip, he never declared how he created the button in the first place. This would be standard though.
@user3158670: tips are preferred in comments on Stack Overflow. Answers are for sure and complete quality posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.