By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,289 Members | 3,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,289 IT Pros & Developers. It's quick & easy.

Tkinter--unexpected behavior with pack_forget()

P: n/a
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
Dec 21 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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():

Dec 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.