[Tkinter-discuss] Removing widgets

Michael Lange klappnase at web.de
Tue May 25 06:36:03 EDT 2004


On Mon, 24 May 2004 07:02:21 -0500
Jeff Epler <jepler at unpythonic.net> wrote:

> You'll need to record the children of 'e' somehow, and destroy them all.
> Or, you can destroy 'e' and start over again.  Or, use 'grid_slaves' or
> 'winfo_children' on 'e' to get a listing of all widgets gridded in 'e', or
> all widgets that are direct children of 'e', and destroy each of them in
> turn.
> 
Another way  might be not to destroy the widgets each time, but to hide them
under the others. I did something similar by creating different subclasses
of Frame and gridding them all in the same place of the main window and
then call tkraise() on the corresponding Frame widget when a new item is selected.
Example:

from Tkinter import *

class Pair(Frame):
    def __init__(self, master, label='pair', **kw):
        Frame.__init__(self, master, **kw)
        self.label = Label(self, text=label)
        self.label.grid(row=0, column=0)
        self.entry = Entry(self)
        self.entry.grid(row=0, column=1)

root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
pair1 = Pair(root, label='pair1')
pair1.grid(row=0, column=0, columnspan=2, sticky='news')
pair2 = Pair(root, label='pair2')
pair2.grid(row=0, column=0, columnspan=2, sticky='news')
b1 = Button(root, text='pair1', command=pair1.tkraise)
b1.grid(row=1, column=0)
b2 = Button(root, text='pair2', command=pair2.tkraise)
b2.grid(row=1, column=1)
root.mainloop()

Best regards

Michael



More information about the Tkinter-discuss mailing list