newbie: why it does not work: command=sys.exit?

P

Paul van Dam

I have got a problem with een example from the book "Programming Python" -
Mark Lutz
I do not understand why it not work for Windows XP and Linux (Fedora Core
1.0).
Exactly the same problem was raised by DrPhil:
http://coderforums.com/showthread.p...695b0a5fcb27197&threadid=2615&goto=nextoldest

import sys
from Tkinter import *
widget = Button(None, text='Hello widget world', command = sys.exit)
widget.pack()
widget.mainloop()
----------------------------------------------------------------------------
--------
Traceback (most recent call last):
File "I:\Python\Projecten\#Prog_in_Python\Hfst06\gui02.py", line 5,
in -toplevel-
widget.mainloop()
File "D:\Python23\lib\lib-tk\Tkinter.py", line 965, in mainloop
self.tk.mainloop(n)
File "D:\Python23\lib\lib-tk\Tkinter.py", line 1347, in __call__
raise SystemExit, msg
SystemExit
 
K

Kirk Strauser

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Paul van Dam said:
import sys
widget = Button(None, text='Hello widget world', command = sys.exit)

Traceback (most recent call last):
File "I:\Python\Projecten\#Prog_in_Python\Hfst06\gui02.py", line 5,
in -toplevel-
widget.mainloop()
File "D:\Python23\lib\lib-tk\Tkinter.py", line 965, in mainloop
self.tk.mainloop(n)
File "D:\Python23\lib\lib-tk\Tkinter.py", line 1347, in __call__
raise SystemExit, msg
SystemExit

- From "pydoc sys.exit"

Help on built-in function exit in sys:

sys.exit = exit(...)
exit([status])

Exit the interpreter by raising SystemExit(status). If the status
is omitted or None, it defaults to zero (i.e., success). If the
status is numeric, it will be used as the system exit status. If it
is another kind of object, it will be printed and the system exit
status will be one (i.e., failure).

The documentation says that calling sys.exit raises a SystemExit exception.
Your program calls sys.exit and gets a SystemExit exception. It seems to be
working as expected and documented, unless I'm missing something.
- --
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAsTXZ5sRg+Y0CpvERAthNAKCSCOtUsv/6eX4TU+ka0c93tOISrQCfRbRz
XvgGK2Kzu137pw4bNlWk7/k=
=mWAd
-----END PGP SIGNATURE-----
 
A

Alan Gauld

I have got a problem with een example from the book "Programming Python" -
Mark Lutz
I do not understand why it not work for Windows XP and Linux (Fedora Core
1.0).
import sys
from Tkinter import *
widget = Button(None, text='Hello widget world', command = sys.exit)
widget.pack()
widget.mainloop()
----------------------------------------------------------------------------
--------
Traceback (most recent call last):
File "I:\Python\Projecten\#Prog_in_Python\Hfst06\gui02.py", line 5,
in -toplevel-
widget.mainloop()
File "D:\Python23\lib\lib-tk\Tkinter.py", line 965, in mainloop
self.tk.mainloop(n)
File "D:\Python23\lib\lib-tk\Tkinter.py", line 1347, in __call__
raise SystemExit, msg
SystemExit

It is working, its raising SystemExit. The environment that you
are running it in (Either Pythonwin or IDLE?) is catching
SystemExit and displaying the message rather than actually exit -
because it would be a pain having to restart IDLE everytime you
ran the program. But if you run your program from Explorer as
normal then it should exit just fine.

Remember that Pythonwin and IDLE are development tools they are
not the normal place to run programs. Because of that they
sometimes behave a little differently in an attempt to make life
easier for you while developing a program.

HTH,

Alan G
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top