4

I have a program that runs in Python, without the console present (which is to say, I've compiled it using py2exe). I want people to be able to quit from the main menu, or by a particular key-press in-game (say, Shift+Q). I'm running it, for now, in Windows, though I am working on compiling Linux/Mac versions. Pressing the X button works if there's no 'while' loop running, it seems, and that closes it correctly, otherwise it seems to 'store' the close command wait until the current loop is closed.

As for menu options, I've looked thoroughly through documentation and Stackoverflow and tried quit(), exit(), sys.exit() and every combination I can find, but every time I get:

Traceback (most recent call last):
  File "alphatime.pyw", line 61177, in <module>
  File "alphatime.pyw", line 53970, in place_menu
NameError: global name 'sys' is not defined

if I try sys.exit, and then:

Traceback (most recent call last):
  File "alphatime.pyw", line 61177, in <module>
  File "alphatime.pyw", line 53970, in place_menu
NameError: global name 'quit' is not defined

if I try just "quit()". I've heard about 'Raising' things like a need to close the program, but I'm not clear what that means (I'm new to Python) and how I would go about doing that.

So, my question is two-fold.

Firstly, is there something I can put in loops for recognizing keypresses something that will recognize the 'X' being clicked, and close?

Secondly, is there an appropriate command that will just close the program? I cannot figure out why these commands don't work, and I've had quite a few complaints from people using the program that it crashes, or they have to ctrl-alt-del it, or whatever. I believe

import os
try:
  os._exit(return_code)
except:
  pass

would work, but at this point, I'm not sure I'm competent enough at python to deploy it appropriately. Thanks in advance!

8
  • Well you could use a signal, or just use 2 threads, the main one would wait for closing event and quit, terminating the other thread that does all the rest. Commented Jul 12, 2012 at 10:43
  • whaat? he said he was a beginner don't confuse him just yet. :) Commented Jul 12, 2012 at 10:46
  • I confess, my eyes glazed a little at that :). But the first problem is now solved, though I'm still puzzled by the 'X' button issue. Commented Jul 12, 2012 at 10:57
  • the x is 'inbuilt'.. it will invoke a quit event whenever it is pressed as long as there is a line that breaks the loop or terminates the program. if you add sys.quit() the x button should work. Commented Jul 12, 2012 at 11:01
  • I'm not sure what you mean by there being a line that breaks the loop. I'm using the libtcod library, and it has a function called wait_for_keypress (self-explanatory). I have a loop that while something is true, it'll wait for a keypress, and the if/elifs that handle it are only for specific keypresses. What would I want to put in there to handle the X? And where would I put the sys.quit()? Thanks! Have done so, also :). Commented Jul 12, 2012 at 11:20

2 Answers 2

5

did you by any chance

import sys

because that should work!

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! I'd been doing "from sys" and getting exit, but not getting all sys. How embarrassing. My thanks! But hitting the 'X' button still doesn't work in the main menu. Is there a way to 'catch' it?
3

NameError: global name 'sys' is not defined

Before you can use sys.exit(), you must import sys.

That's the best way to exit the program. Function names that begin with _ are considered internal, and should not be used unless you are really trying to do something weird.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.