4

I have written this question after reading this question and this other one. I would like to stop the execution of a Python script when a button is pressed. Here the code:

import turtle
from sys import exit

def stop_program():
    print("exit function")
    exit(0) #raise SystemExit(0) gives the same result
    print("after the exit function")

# Create keyboard binding
turtle.listen()
turtle.onkey(stop_program, "q")

# Main function
while True:
    # Code: everything you want

If I press the button "q" (even muliple time) the output is:

exit function
exit function
exit function
exit function
exit function
exit function
exit function
...

i.e. one line every time I press. This means that the exit works for the function and not for the whole program. Any suggestion?

8
  • have you tried turtle.bye instead of your stop_program function? docs.python.org/3.3/library/… Commented Jan 4, 2019 at 14:23
  • trying now, however, what I wrote should work. The question is: why it exit from the function and not from the script execution? it is not a "return" Commented Jan 4, 2019 at 14:33
  • @Andrew: it exists but with some errors (it seems because of it is executing something else in the middle) Commented Jan 4, 2019 at 14:38
  • 2
    Can I see what you have under while True:? Do you happen to do except:\n anywhere? Commented Jan 4, 2019 at 14:57
  • 2
    I have found why this happens: "Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.". I am investigating to find a "clean" solution (details from the sys module documentation) Commented Jan 4, 2019 at 15:21

2 Answers 2

2

Dont use the while loop, use turtle.mainloop()

import turtle
from sys import exit

def stop_program():
    print("exit function")
    exit(0) #raise SystemExit(0) gives the same result
    print("after the exit function")

# Create keyboard binding
turtle.listen()
turtle.onkey(stop_program, "q")


turtle.mainloop()

That seems to work fine for me, give it a try.

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

2 Comments

where is the definition of turtle.mainloop()? where I can insert my instruction? I will try to find more info surfing on google
-1

Try to use: sys.exit(), see if that works. Below code worked for me.

import turtle
import sys

def stop_program():
 print("exit function")
 sys.exit() #raise SystemExit(0) gives the same result
 print("after the exit function")


 # Create keyboard binding
 turtle.listen()
 turtle.onkey(stop_program, "q")
 turtle.mainloop()

5 Comments

of course no: from sys import exit allow to write just exit instead of sys.exit(). It is the same, isn't it?
import sys and then use sys.exit(). It woks for me fine. Used mikeg's code above, but adapted it a bit with my suggestion.
did you try the code that I wrote in the question? and it works? really?
It does not work. Try the code in the question please
I edited my solution to show you what worked for me. Have a look and let me know.

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.