Tkinter--unexpected behavior with pack_forget()

K

Kevin Walzer

I'm trying to toggle the visibility of a Tkinter widget using
pack_forget(), and I'm running into unexpected behavior. The widget
"hides" correctly, but does not become visible again. My sample code is
below:

----
from Tkinter import *

root = Tk()

label = Label(text="Hello")
label.pack()

def toggle():
if label.winfo_ismapped:
print "mapped"
label.pack_forget()
else:
label.pack()

button = Button(text="Push me", command=toggle)
button.pack()

root.mainloop()
-----

The expected behavior is for the label to hide ("pack_forget()") if
label.winfo_ismapped returns true. That works as . However, I would also
expect the widget to be displayed/packed if it is not mapped. What
happens instead is that the label is apparently mapped even after I call
label.pack_forget().

Can anyone point out what I'm doing wrong? Calling "pack forget" from
Tcl maps/unmaps the widget as expected. In this case, I can't quite
figure out where my error is.
 
J

John McMonagle

Kevin said:
I'm trying to toggle the visibility of a Tkinter widget using
pack_forget(), and I'm running into unexpected behavior. The widget
"hides" correctly, but does not become visible again. My sample code is
below:

----
from Tkinter import *

root = Tk()

label = Label(text="Hello")
label.pack()

def toggle():
if label.winfo_ismapped:
print "mapped"
label.pack_forget()
else:
label.pack()

button = Button(text="Push me", command=toggle)
button.pack()

root.mainloop()
-----

The expected behavior is for the label to hide ("pack_forget()") if
label.winfo_ismapped returns true. That works as . However, I would also
expect the widget to be displayed/packed if it is not mapped. What
happens instead is that the label is apparently mapped even after I call
label.pack_forget().

Can anyone point out what I'm doing wrong? Calling "pack forget" from
Tcl maps/unmaps the widget as expected. In this case, I can't quite
figure out where my error is.



if label.winfo_ismapped: always evaluates to True.

Instead, you need,

if label.winfo_ismapped():
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top