[Tkinter-discuss] Adding/removing widgets from a running application?

Jim Kleckner jek-gmane at kleckner.net
Thu Feb 21 17:20:06 CET 2008

Martin Franklin wrote:
> Russell E. Owen wrote:
>> In article <foqjpn$eff$1 at ger.gmane.org>,
>>  Alexander Belchenko <bialix at ukr.net> wrote:
>>> Hash: SHA1
>>> Jim Kleckner пишет:
>>> | Any pointers to what it takes to add/remove widgets
>>> | from a running application?
>>> Something like this maybe?
>>> from Tkinter import *
>>> root = Tk()
>>> x = Label(root, text='Hello')
>>> x.pack()
>>> Button(root, text='Hide', command=x.pack_forget).pack()
>>> root.mainloop()
>> Or if you use the gridder you can remove things temporary and restore 
>> them again easily:
>>   x.grid(row=0....)
>>   x.grid_remove() # to temporarily remove
>>   x.grid() # to restore using the original grid settings
>>   x.grid_forget() # to remove permanently
>> (remove is not supported by the packer)
> pack_forget does not erm remove either ;)
> from Tkinter import *
> root = Tk()
> root.title("Pack is BEST!!!")
> l = Label(root, text="Pack is simply the best")
> l.pack()
> def forget():
>      l.pack_forget()
> def remember():
>      l.pack()
> b = Button(root, text="Forget about it", command=forget)
> b.pack()
> b = Button(root, text="Wait, I still need you", command=remember)
> b.pack()
> root.mainloop()
> BUT... the remember function packs the label widget in it's new packing 
> order, so it is packed after the two buttons, as far as I know the only
> way to 'remember' its pack position is to put it inside a Frame widget
> (and leave the Frame where it stands) - at least thats how I do it
> To the OP I guess you've seen how easy this is with Tk(inter) :)
> Cheers,
> Martin.

Thank you for the nice examples.

I see running this application that the remember button
causes the label to relocated to the bottom of the window.

What I actually want is to construct and add new widgets
during the mainloop().  So a button that appends new labels
at the bottom based on a text entry field would demonstrate
the issue.

I've been playing with this extremely simple and interesting
tVector helper code from here:

Ideally, I just want to reach in and extend the widget list.
I'm attaching a file with some concept code using tVector
(yes, I should rewrite it with grid, I guess).

The grid manager looks interesting and it seems that it
ought to be able do something like this but the methods
don't obviously have an append() or some such.


More information about the Tkinter-discuss mailing list