How to terminate a TkinterApp correctly?

G

Gregor Lingl

I'm working on a windows machine

I've written a Tkinter-app (sort of game) which
consists mainly of an animation which is driven
by a while True: ... loop.

If I close the App's window by clicking the
right upper standard-X-Button, the program
doesn't terminate cleanly. Instead a somewhat
cryptic error message is displayed, e.g.:

.....
TclError: invalid command name ".12880040.12880944"

which - I suppose - stems from the interpreter trying
to execute some statement in the infinite loop.

Only if this loop is terminated by some other means
- e.g. game over - before closing the window no
error message is displayed.

How, i.e. by what sort of event handler or error handler
can I avoid this annoying behaviour of my program?

Regards, Gregor
 
M

Michael Peuser

Gregor Lingl said:
I'm working on a windows machine

I've written a Tkinter-app (sort of game) which
consists mainly of an animation which is driven
by a while True: ... loop.
[....]
Only if this loop is terminated by some other means
- e.g. game over - before closing the window no
error message is displayed.

Correct

How, i.e. by what sort of event handler or error handler
can I avoid this annoying behaviour of my program?

You can bind <Destroy> and act accordimngly, or intercept the action
altogether.

Example:
-----------------------------
from Tkinter import *

def killingAction(ev):
print "Destroyed"

def kidding():
print "not killing"
l.config(text="just kidding")
l.master.protocol("WM_DELETE_WINDOW",original)

l=Label(text="Kill me!")
l.pack()
l.bind("<Destroy>",killingAction)

original= l.master.protocol("WM_DELETE_WINDOW",None)
l.master.protocol("WM_DELETE_WINDOW",kidding)
l.master.destroy=lambda: 0

mainloop()
 
M

Michael Peuser

I just notice a left over line from a test, that lambda is of no use at all!
May be you will have to use WM_SAVE_YOURSELF instead of WM_DELETE_WINDOWS if
you want to be sure to keep the widget absolutly intact. Between these two
messages some tidying-up could aleady have happened behind the scene....So
this is my fine example:

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

def killingAction(ev):
print "Destroyed"

def kidding():
print "not killing"
l.config(text="just kidding")
l.master.protocol("WM_DELETE_WINDOW",original)

l=Label(text="Kill me!")
l.pack()
l.bind("<Destroy>",killingAction)

original= l.master.protocol("WM_DELETE_WINDOW",None)
l.master.protocol("WM_DELETE_WINDOW",kidding)


mainloop()
 
M

Michael Peuser

----- Original Message -----
From: "Gregor Lingl" <[email protected]>
Newsgroups: comp.lang.python
Sent: Saturday, August 16, 2003 3:53 PM
Subject: Re: How to terminate a TkinterApp correctly?

.....
Thanks for your remarks and your example. The
following solution finally did it:

def exit():
global done
done = True # so the animation will terminate, but
# not immediately! The actual pass through
# the loop has to be finished.
print "done!"
import sys
# after_idle seems to be crucial! Waits for terminating
# the loop (which is in a callback function)
cv.after_idle(sys.exit, (0,))


Why don't you just return? The mainloop can handle everything!?
Example:
def exit(self):
self.done=1
return

(If you can put it into an appropriate class ....)
 
J

John Roth

Michael Peuser said:
----- Original Message -----
From: "Gregor Lingl" <[email protected]>
Newsgroups: comp.lang.python
Sent: Saturday, August 16, 2003 3:53 PM
Subject: Re: How to terminate a TkinterApp correctly?

....



Why don't you just return? The mainloop can handle everything!?

Unfortunately, the combination of Windows 9x (including Me)
and Python 2.1 can't handle everything. It may also be broken
in 2.2 and later, but I quit trying after reaching my frustration
limit. If you exit with an exception, something doesn't clean up
properly, and you break the DOS box and eventually Windows
itself when you try to shut down.

This problem doesn't exist in the Windows 2K (including XP)
code base, or other variants.

I don't know that it's ever been solved, but considering that
Windows 9x is gradually going away, it's also not a real hot
priority.

John Roth
 
M

Michael Peuser

John Roth said:
Unfortunately, the combination of Windows 9x (including Me)
and Python 2.1 can't handle everything. It may also be broken
in 2.2 and later, but I quit trying after reaching my frustration
limit. If you exit with an exception, something doesn't clean up
properly, and you break the DOS box and eventually Windows
itself when you try to shut down.

This problem doesn't exist in the Windows 2K (including XP)
code base, or other variants.

I don't know that it's ever been solved, but considering that
Windows 9x is gradually going away, it's also not a real hot
priority.


This was not my point I think.... I was not referring to tk/system mainloop
but to your own loop you mentioned. This is where you set "done=1" for...
The after_idle is confusing and probably not what you want. I have the
impression that there is still some misunderstanding.

It is absolutly fine to intercept the user click to the close box - there is
no magic it and - as to my example - you can do what you want for hours
after. Note: the "closing" process is stopped, when you use this WM-....
trick. (Because I was not *quite* sure about the internal states, I also
recommende to use WM_SAVE_YOURSELF instead).

But I think it is not worth all the work - and still unsafe! - to just call
sys.exit() !!!

Kindly Michasel P
 
G

Gregor Lingl

Michael said:
----- Original Message -----
From: "Gregor Lingl" <[email protected]>
Newsgroups: comp.lang.python
Sent: Saturday, August 16, 2003 3:53 PM
Subject: Re: How to terminate a TkinterApp correctly?

....





Why don't you just return? The mainloop can handle everything!?
Example:
def exit(self):
self.done=1
return
Here I apparently don't understand something fundamental:
what is the effect of a return statement as the last statement
of a function?
Moreover: If I put this into the cv.master.protocol ... I cannot
close the application at all. (Because the original is not restored.
And I don't want to need a second mouseclick as in your first example.)
Gregor
 
G

Gregor Lingl

Michael said:
....



This was not my point I think.... I was not referring to tk/system mainloop
but to your own loop you mentioned.

The above answer was not mine, so there is samething a bit mangled ...

This is where you set "done=1" for...
The after_idle is confusing and probably not what you want.

But it worked!

I have the
impression that there is still some misunderstanding.
Maybe!

It is absolutly fine to intercept the user click to the close box - there is
no magic it and - as to my example - you can do what you want for hours
after. Note: the "closing" process is stopped, when you use this WM-....
trick. (Because I was not *quite* sure about the internal states, I also
recommende to use WM_SAVE_YOURSELF instead).

But I think it is not worth all the work - and still unsafe! -

WHY?

to just call
sys.exit() !!!

So I found another working solution to my problem:
I put the code for the go-function, which is the command of a goButton
in a try:- except: clause.

def go():
global done
try:
resetGame()
goButton["state"] = Tk.DISABLED
while fehler < MAXFEHLER and not done:
for huhn in huehner:
huhn.move()
cv.update()
done = True
goButton["state"] = Tk.NORMAL
except:
pass

This catches the TclError and everything terminates regularly
Regards, Gregor
 
M

Michael Peuser

Gregor Lingl said:
Michael Peuser schrieb:
.....

Here I apparently don't understand something fundamental:
what is the effect of a return statement as the last statement
of a function?

Nothing, just an indicator to make clear that this is the end of the
procedure...
Moreover: If I put this into the cv.master.protocol ... I cannot
close the application at all. (Because the original is not restored.
And I don't want to need a second mouseclick as in your first example.)


This is a misunderstanding. Closing your application has nothing to do with
closing some window and even less with where a stupid user clicks on ;-)

You can destroy a widget with "destroy"; when the root widget is destroyed
then the Tkinter mainloop terminats by convention. Nothing to do with your
program!!


See this example:
---------------------------
from Tkinter import *

def killingAction():
l.master.destroy()

def kidding():
l.config(text="just kidding")


l=Button(text="Kill me!",command=killingAction)
l.pack()

l.master.protocol("WM_DELETE_WINDOW",kidding)


mainloop()
print "here we are- ready for whatever we want"
-----------------------
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top