473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

tkinter wm_delete_window

hello i want to intercept tkinter python system events like
wm_delete_window
and if possible for any window, but the newest code I've produced give
me
an error :

Traceback (most recent call last):
File "C:\Documents and Settings\yvesd\Bureau\protowin.py", line 36,
in ?
b1 = Tkinter.Button (win1)
File "C:\Python24\lib\lib-tk\Tkinter.py", line 1933, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Python24\lib\lib-tk\Tkinter.py", line 1856, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python24\lib\lib-tk\Tkinter.py", line 1834, in _setup
self.tk = master.tk
AttributeError: MyDlg instance has no attribute 'tk'

thanks a lot for the help answer.

here is my code :

import Tkinter
from Tkinter import *

class MyDlg(Toplevel):
def ma_fonction():
print "my function is finished."
def __init__(arg1, arg2):
arg1.protocol("WM_DELETE_WINDOW", arg1.ma_fonction)

root = Tkinter.Tk ()
#win1 = Tkinter.Toplevel (root)
win1 = MyDlg(root)
b1 = Tkinter.Button (win1)
b1.config (text="hello")

Jul 18 '06 #1
1 5182
On Tue, 18 Jul 2006 11:16:04 +0200, yvesd <yv************@gmail.comwrote:
hello i want to intercept tkinter python system events like
wm_delete_window
and if possible for any window, but the newest code I've produced give
me
an error :
Traceback (most recent call last):
File "C:\Documents and Settings\yvesd\Bureau\protowin.py", line 36,
in ?
b1 = Tkinter.Button (win1)
File "C:\Python24\lib\lib-tk\Tkinter.py", line 1933, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Python24\lib\lib-tk\Tkinter.py", line 1856, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python24\lib\lib-tk\Tkinter.py", line 1834, in _setup
self.tk = master.tk
AttributeError: MyDlg instance has no attribute 'tk'
thanks a lot for the help answer.
here is my code :
import Tkinter
from Tkinter import *
Do not import the same module twice: either use "import Tkinter" and
prefix everything in it with 'Tkinter.', or use "from Tkinter import *"
and don't specify any prefix. You're mixing both here.
class MyDlg(Toplevel):
def ma_fonction():
You must specify 'self' as first parameter here. Or put 'ma_fonction'
outside the class.
print "my function is finished."
def __init__(arg1, arg2):
arg1.protocol("WM_DELETE_WINDOW", arg1.ma_fonction)
Please don't use anything else than 'self' as the name for methods' first
parameter. It will confuse everyone who knows Python. And your mistake is
here: the constructor for the super-class (Toplevel) is not called
automatically by the sub-class constructor; so you have to do it yourself.
So just rewrite these two lines as:

def __init__(self):
Toplevel.__init__(self)
self.protocol("WM_DELETE_WINDOW", self.ma_fonction)

and everything should work fine. BTW, I've removed the parameter arg2 that
you didn't use at all.
root = Tkinter.Tk ()
#win1 = Tkinter.Toplevel (root)
win1 = MyDlg(root)
Replace that with:

win1 = MyDlg()
b1 = Tkinter.Button (win1)
b1.config (text="hello")
An additional small note: you can write the last two lines as a single one:

b1 = Tkinter.Button(win1, text='hello')

And you also must call:
- b1.pack, b1.grid or b1.place to put your button somewhere in your window.
- root.mainloop() in the end or your window will never appear.

If you want to do Tkinter the OO way, you may also create the button in
the dialog's constructor.

So here would be my version of your program:

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

class MyDlg(Toplevel):
def __init__(self):
Toplevel.__init__(self)
b1 = Button(self, text='hello')
b1.pack()
self.protocol("WM_DELETE_WINDOW", self.ma_fonction)
def ma_fonction(self):
print "my function is finished."

root = Tk()
win1 = MyDlg()
root.mainloop()
----------------------------------------------

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Jul 18 '06 #2

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

Similar topics

2
by: Rob | last post by:
My first GUI so be gentle... When I start my program I call a class that runs the initial window. While in this class if a certain button is pressed it calls a function outside the class. This...
3
by: DoubleM | last post by:
Hi, I'm running Python2.3.3c1 on Mandrake 9.1 The following code is designed to bring up a window with a button labeled "popup". Clicking on the popup buttons triggers a secondary window with...
3
by: Mickel Grönroos | last post by:
Hi everybody, I'm using QuickTimeTcl (3.1) to be able to play movie files in my Tkinter application (Python 2.3.2) on Windows 2000. I was planning to write a simple wrapper class,...
2
by: Russell E. Owen | last post by:
I want to support execution of simple user-written scripts in a Tkinter application. The scripts should be able to wait for data and such without hanging the GUI (and without having to write the...
1
by: Pekka Niiranen | last post by:
Hi there, after reading TkInter/thread -recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965 I wondered if it was possible to avoid using threads for the following problem: ...
6
by: Peter Kleiweg | last post by:
I have an application written in Tkinter. There is a menu item 'quit' that calls the function 'quit'. If 'quit' is called, it first checks if there is unsaved data. If there is, it won't let the...
4
by: Bob Greschke | last post by:
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...
5
by: half.italian | last post by:
Hi all, I don't really understand how to properly use threading in my programs, however I have managed to get by so far using them improperly. Once again I have come up to what I think is...
0
by: nicstel | last post by:
Hello. My script run fine within python but not in my program(SDS/2 wich is a software like Autocad). The problem is I got an error when the time comes to read the line14 to 19. (Source code come...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.