how best to clear objects from a frame

Chris Hare chare at labr.net
Sun Aug 1 20:12:13 EDT 2010


Here is the situation:

I have a window with a bunch of widgets in it.  I want to clear the objects in a given frame and recreate them to update them.  

The example below destroys the top level frame, and so I can't recreate the widgets.  I am likely doing this wrong.
should I be doing this in a class?


Thanks for the help.

from Tkinter import *

def createLeftWidgets(left):
	#
	# Add the Net Status Section
	#
	conditions = LabelFrame(left, text="Net Conditions",bg="Gray")
	conditions.grid(row=0,column=0);
	button = Button(conditions, text="Refresh", command=refreshNetConditions, highlightbackground="Green")
	button.grid(row=0,column=1, sticky=E)
	cText = Text(conditions,bg="Gray")
	cText.insert(END, root.netHistory.get())
	cText.config(height=12,width=40)
	cText.grid(row=1,column=0,columnspan=2)
	status = LabelFrame(left, text="Net Details",bg="Gray")
	status.grid(row=1,column=0,sticky=N+E+W+S);
	lblNetNumber = Label(status, text="Net Number")
	lblNetNumber.grid( row=19, column=0, columnspan=2,sticky=W)
	return(conditions)
def refreshNetConditions():
	global frameLeft
	frameLeft.destroy()
	root.netHistory.set( "inserting text\n" + root.netHistory.get())
	createLeftWidgets(frameLeft)

root = Tk()
root.netHistory = StringVar()
root.netHistory.set("goes into the text widget")
frame = Frame(root)
frame.grid()
frameLeft = createLeftWidgets(frame)

root.mainloop()




More information about the Python-list mailing list