Tkinter--unexpected behavior with pack_forget()

Kevin Walzer kw at codebykevin.com
Thu Dec 20 20:10:40 EST 2007


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.

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com



More information about the Python-list mailing list