Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to terminate a TkinterApp correctly? (http://www.velocityreviews.com/forums/t321071-how-to-terminate-a-tkinterapp-correctly.html)

Gregor Lingl 08-16-2003 09:44 AM

How to terminate a TkinterApp correctly?
 
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


Michael Peuser 08-16-2003 10:24 AM

Re: How to terminate a TkinterApp correctly?
 

"Gregor Lingl" <glingl@aon.at> schrieb im Newsbeitrag
news:3F3DFCF4.5020109@aon.at...
> 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()
--------------------------------------------

Kindly
Michael P


> Regards, Gregor
>




Michael Peuser 08-16-2003 10:34 AM

Re: How to terminate a TkinterApp correctly?
 
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()
-------------------------------------------

Kindly
Michael P




Michael Peuser 08-16-2003 04:35 PM

Re: How to terminate a TkinterApp correctly?
 

----- Original Message -----
From: "Gregor Lingl" <glingl@aon.at>
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 ....)

>
> cv.master.protocol("WM_DELETE_WINDOW", exit)
>
> if not usingIDLE:
> root.mainloop()
>
> Regards, Gregor
>




John Roth 08-16-2003 06:06 PM

Re: How to terminate a TkinterApp correctly?
 

"Michael Peuser" <mpeuser@web.de> wrote in message
news:bhlmgq$dqk$02$1@news.t-online.com...
>
> ----- Original Message -----
> From: "Gregor Lingl" <glingl@aon.at>
> 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!?


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




Michael Peuser 08-16-2003 06:35 PM

Re: How to terminate a TkinterApp correctly?
 

"John Roth" <newsgroups@jhrothjr.com> schrieb im Newsbeitrag
news:vjsslisoeitsc9@news.supernews.com...
>
> "Michael Peuser" <mpeuser@web.de> wrote in message
> news:bhlmgq$dqk$02$1@news.t-online.com...
> >
> > ----- Original Message -----
> > From: "Gregor Lingl" <glingl@aon.at>
> > 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!?

>
> 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


> John Roth
>
>
>




Gregor Lingl 08-16-2003 07:24 PM

Re: How to terminate a TkinterApp correctly?
 
Michael Peuser schrieb:

> ----- Original Message -----
> From: "Gregor Lingl" <glingl@aon.at>
> 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
>

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
> (If you can put it into an appropriate class ....)
>
>
>>cv.master.protocol("WM_DELETE_WINDOW", exit)
>>
>>if not usingIDLE:
>> root.mainloop()
>>
>>Regards, Gregor
>>

>
>
>



Gregor Lingl 08-16-2003 07:34 PM

Re: How to terminate a TkinterApp correctly?
 
Michael Peuser schrieb:

> "John Roth" <newsgroups@jhrothjr.com> schrieb im Newsbeitrag

....

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

>>
>>Unfortunately,


....
>>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.


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

> Kindly Michasel P
>
>
>
>>John Roth
>>
>>
>>

>
>
>



Michael Peuser 08-16-2003 07:44 PM

Re: How to terminate a TkinterApp correctly?
 

"Gregor Lingl" <glingl@aon.at> schrieb im Newsbeitrag
news:3F3E84DA.8040905@aon.at...
> Michael Peuser schrieb:


.....

> > 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?


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"
-----------------------





All times are GMT. The time now is 08:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.