Clearing the Tkinter Window

James Stroud jstroud at ucla.edu
Thu Dec 15 17:34:36 EST 2005


Dustan wrote:
> I'm a newbie here, especially with Tkinter.  I'm writing a program that
> has 3 phases, if you will, in which I would have to clear the window
> and insert new widgets. Is this possible, and if so, how?  I'm writing
> my application class based on Frame, if that helps at all.
> 

Its not obvious what you are asking.

But, if you populate a Frame with widgets, then you can destroy those 
widgets if you destroy the Frame:

tk = Tk()
f = Frame(tk)

# fill frame with 10 buttons
for i in xrange(10):
   Button(f, text=str(i)).pack()

# kill the frame and buttons
f.destroy()   # references to buttons gone too, so they are GC'd

# make a new frame and put it in tk
f = Frame(tk)

# etc.

You will want to make sure any name assignments are re-assigned, del'd, 
or go out of scope after you destroy frame, or the button objects won't 
be garbage collected, even though they are destroyed when the frame is 
destroyed.

James



More information about the Python-list mailing list