Getting Tkinter Text contents before destruction

James Stroud jstroud at ucla.edu
Mon Feb 6 22:32:52 EST 2006


Bob Greschke wrote:
> Hi!
> 
> I want to grab the contents of a Text widget when the frame it's on gets 
> destroyed.  I tried TextWidget.bind("<Destroy>"... , but the widget is gone 
> before the call gets made, and I'd really hate to do something with the 
> function that gets called with TextWidgetsFrame.bind("<Destroy>", ..., since 
> that one function handles all of the frames in the program...or would that 
> even work?
> 
> How can I do this?
> 
> Thanks!
> 
> Bob
> 
> 

If TextWidgetsFrame inherets from frame, you can override the destroy() 
method which gets called when the parent gets destroyed. Or 
alternatively, you can override the __del__ function, which gets called 
when the reference count goes to 0:


py> from Tkinter import *
py> tk = Tk()
py> class F(Frame):
...   def destroy(self):
...     print 'bob'
...     Frame.destroy(self)
...
py> f = F(tk)
py> f.pack()
py> tk.destroy()
bob

James



More information about the Python-list mailing list