Getting Tkinter Text contents before destruction

B

Bob Greschke

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
 
J

James Stroud

Bob said:
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
 
E

Eric Brunel

If TextWidgetsFrame inherets from frame, you can override the destroy()
method which gets called when the parent gets destroyed.

Unfortunately, it doesn't get called. Everything actually happens at tk
level, where the destroy *command* gets called, but doesn't inform the
Python interface object. The destroy method is only a way to call tk's
destroy command, but overloading it has an effect only when called from
Python, not from the underlying tk layer.
 
F

Fredrik Lundh

Bob said:
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?

in what ways can the frame be destroyed ?

assuming that you're talking about user-initiated actions, the most reasonable way
to do this is to implement a WM_DELETE_WINDOW handler on the toplevel
window that the frame is located in, and deal with the text widget in there. see:

http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm#protocols

</F>
 
E

Eric Brunel

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?

One way is to define the deletion callback for the text's parent window to
get the text before the widget gets deleted. To do that, you can use
text.winfo_toplevel() to get the parent Toplevel for your text widget,
then define the callback via wdw.protocol('WM_DELETE_WINDOW', ...). Here
is a detailed example:

-----------------------------------------------------
from Tkinter import *

root = Tk()

txt = None

def openWdw():
global txt
wdw = Toplevel()
frm = Frame(wdw)
frm.pack(expand=1)
txt = Text(frm)
txt.pack()
print txt.winfo_toplevel(), frm, root
txt.winfo_toplevel().protocol('WM_DELETE_WINDOW', getText)

def getText():
print txt.get(1.0, END)
txt.winfo_toplevel().destroy()

Button(root, text='Go', command=openWdw).pack()

root.mainloop()
-----------------------------------------------------

This will of course only work if the only reason for which the text widget
can be destroyed is if its parent window is closed.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,475
Latest member
NovellaSce

Latest Threads

Top