473,386 Members | 1,835 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,386 software developers and data experts.

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

Jul 18 '05 #1
8 5091

"Gregor Lingl" <gl****@aon.at> schrieb im Newsbeitrag
news:3F**************@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

Jul 18 '05 #2
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

Jul 18 '05 #3

----- Original Message -----
From: "Gregor Lingl" <gl****@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

Jul 18 '05 #4

"Michael Peuser" <mp*****@web.de> wrote in message
news:bh*************@news.t-online.com...

----- Original Message -----
From: "Gregor Lingl" <gl****@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

Jul 18 '05 #5

"John Roth" <ne********@jhrothjr.com> schrieb im Newsbeitrag
news:vj************@news.supernews.com...

"Michael Peuser" <mp*****@web.de> wrote in message
news:bh*************@news.t-online.com...

----- Original Message -----
From: "Gregor Lingl" <gl****@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

Jul 18 '05 #6
Michael Peuser schrieb:
----- Original Message -----
From: "Gregor Lingl" <gl****@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



Jul 18 '05 #7
Michael Peuser schrieb:
"John Roth" <ne********@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



Jul 18 '05 #8

"Gregor Lingl" <gl****@aon.at> schrieb im Newsbeitrag
news:3F**************@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"
-----------------------

Jul 18 '05 #9

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

Similar topics

3
by: abovetreeline | last post by:
Hi, How to make javascript terminate a win32_process? vbscript will terminate a named process with the following code: strcomputer = "." Set objwmiservice = GetObject("winmgmts:" _ &...
9
by: liangbowen | last post by:
As i konw, the only way to to terminate a thread started by _beginthread() is to user _endthread() (or return) inside the thread. now i started a thread(TodoThread), and created a listenning...
4
by: Dr. J | last post by:
How to terminate a blocked thread? In my form's "load" I launch a TCP listening thread that stays in an infinite loop waiting for incoming TCP packets. In this form's "closing" I try to...
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
3
by: Keith Grefski | last post by:
I cant seem to figure out how to terminate a process in vb.net i can do it using vbscript and wmi but terminate doesnt seem to exist in the available classes for wmi under vb.net
3
by: Peter Neuburger via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Peter Neuburger Hi Everybody, I need some help with the WMI Terminate Method. I have a Listbox where I get all the Process...
7
by: fyi85 | last post by:
I have 8.1.5 on Windows 2003, when I do from CLP with auto commit off: db2 update table set column=something and then db2 terminate and then db2 connect to db db2 select updated column from...
0
by: Scheu | last post by:
Hi all, I'm hoping someone can help me with this bizarre problem. If I create a new PerformanceCounter object in a ComVisible class and invoke it from wscript, it never seems to terminate. When...
1
by: Justin | last post by:
We had a load that failed. Now we have a pending load. When executing load terminate, we receive the following error: db2 load terminate SQL0104N An unexpected token "terminate" was found...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.