473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting Tkinter Text contents before destruction

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
Feb 7 '06 #1
4 2198
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
Feb 7 '06 #2
On Mon, 06 Feb 2006 19:32:52 -0800, James Stroud <js*****@ucla.edu> wrote:
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.
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
Feb 9 '06 #3
On Mon, 6 Feb 2006 20:11:28 -0700, Bob Greschke <bo*@greschke.com> 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?


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
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
Feb 9 '06 #4
Bob Greschke wrote:
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/tkinte....htm#protocols

</F>

Feb 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3193
by: Eric Brunel | last post by:
Hi all, I was creating a Tkinter widget in the style of the reversed tabs below Excel worksheets and I stepped in a serious problem: the code I made makes python crash with a seg fault, bus...
2
4608
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
4
5254
by: Patrick L. Nolan | last post by:
Our Tkinter application has a big ScrolledText widget which is a log of everything that happens. In order to prevent people from making changes, we have it in DISABLED mode except when the...
1
3836
by: corrado | last post by:
Hello I have an application running several thread to display some financial data; basically I have a thread displaying HTML tables by means of Tkhtml, another implementing a scrolling ticker...
4
5049
by: Ivo Woltring | last post by:
Hi Pythoneers, I am trying to make my own gui for mencoder.exe (windows port of the terrific linux mencoder/mplayer) to convert divx to Pocket_PC size. My current app creates a batch script to...
5
2029
by: max(01)* | last post by:
hello. the following code: 1 from Tkinter import * 2 3 class MiaApp: 4 def __init__(self, genitore): 5 self.mioGenitore = genitore 6 self.i = IntVar()
2
2471
by: import newbie | last post by:
Hi all, I'm a programming dabbler trying learn Python, and I've got a few questions. Mainly: Where can I find a good open-source library or tutorial (preferably free) that explains how to...
0
2337
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
5
6826
by: exhuma.twn | last post by:
As many might know, windows allows to copy an image into the clipboard by pressing the "Print Screen" button on the keyboard. Is it possible to paste such an image from the clipboard into a "Text"...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.