3

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?

Leos313
  • 5,152
  • 6
  • 40
  • 69
  • have you tried `turtle.bye` instead of your `stop_program` function? https://docs.python.org/3.3/library/turtle.html?highlight=turtle#turtle.bye – Andrew Jan 04 '19 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" – Leos313 Jan 04 '19 at 14:33
  • @Andrew: it exists but with some errors (it seems because of it is executing something else in the middle) – Leos313 Jan 04 '19 at 14:38
  • 2
    Can I see what you have under `while True:`? Do you happen to do `except:\n` anywhere? –  Jan 04 '19 at 14:57
  • after the `while True:` there are other functions defined by me. For simplicity assume that they are just `print()` (I have tried and it does not exit either). However, what do you mean with `except:\n`? – Leos313 Jan 04 '19 at 15:05
  • I was thinking if there were a bare `except:` (as opposed to `except Exception`) that would explain why you're not exiting all the way. This is really weird. –  Jan 04 '19 at 15:11
  • 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) – Leos313 Jan 04 '19 at 15:21
  • I am realizing that it IS the main thread (but not the main function). So, probably, the reason is not the one I was pointing out in the previous comment – Leos313 Jan 04 '19 at 15:25

2 Answers2

1

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.

mikeg
  • 444
  • 3
  • 13
  • where is the definition of turtle.mainloop()? where I can insert my instruction? I will try to find more info surfing on google – Leos313 Jan 04 '19 at 15:11
  • it is built into turtle, https://docs.python.org/3.1/library/turtle.html#turtle.mainloop – mikeg Jan 04 '19 at 15:12
-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()