473,568 Members | 3,014 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 TextWidgetsFram e.bind("<Destro y>", ..., 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 2205
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 TextWidgetsFram e.bind("<Destro y>", ..., 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 TextWidgetsFram e 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(s elf)
....
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.e du> wrote:
If TextWidgetsFram e 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.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"
Feb 9 '06 #3
On Mon, 6 Feb 2006 20:11:28 -0700, Bob Greschke <bo*@greschke.c om> 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 TextWidgetsFram e.bind("<Destro y>", ...,
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_topl evel() to get the parent Toplevel for your text widget,
then define the callback via wdw.protocol('W M_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_tople vel(), frm, root
txt.winfo_tople vel().protocol( 'WM_DELETE_WIND OW', getText)

def getText():
print txt.get(1.0, END)
txt.winfo_tople vel().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.zmz 5(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 TextWidgetsFram e.bind("<Destro y>",
..., 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_WINDO W 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
3197
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 error or X11 BadGC error on both Solaris (2.6 and 2.7) and Linux (Mandrake 8.0); it doesn't crash on Windows. I tried to simplify the script, but I...
2
4612
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 turn the contents of the 3rd list (call it states would be updated by a change in the 2nd list. If anyone can share a recipe or some ideas, I'd be...
4
5279
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 program wants to write a new entry. This works OK, except that sometimes we want to copy out of piece of the contents and paste it in another window. ...
1
3840
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 based on a Text widget with embedded windows and a thread running the Tkinter mainloop plus several other thread dealing with the scheduling of the...
4
5058
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 run the mencoder with the needed params, but now I want to integrate mencoder as a subprocess in my app. What already works: the starting of the...
5
2043
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
2478
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 easily manipulate text in a tKinter window? Basically, I want to be able to do anything that HTML can do (or close to it) but without the HTML. :-)
0
2341
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 widget, but for some reason the text is invisible on cygwin). After I close the Toplevel widget, all of the menus in my app behave as though they...
5
6836
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" widget in Tkinter? Here is my first attempt with just trying to print out the image data: ----------------- def pasteImg(tgt): global...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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 we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.