Tkinter--unexpected behavior with pack_forget()

John McMonagle jmcmonagle at velseis.com.au
Thu Dec 20 21:49:41 EST 2007


Kevin Walzer wrote:
> 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():




More information about the Python-list mailing list