Possible Duplicates:
Terminating a Python script
Terminating a Python Program
My question is how to exit out in Python main function? I have tried 'return
' but it gave the error SyntaxError: 'return' outside function
. Can anyone help? Thanks.
if __name__ == '__main__': try: if condition: (I want to exit here) do something finally: do something
exit() function raises a SystemExit exception to exit the program, so try statements and cleanup code can execute.
Break up the loop. Have it just do one iteration (or, say, 100 iterations, if they're really quick), and then use after or after_idle to ask Tkinter to call a function that does another one iteration (or 100 iterations) and after s again, and so on, until they're all done.
You can use sys.exit()
to exit from the middle of the main function.
However, I would recommend not doing any logic there. Instead, put everything in a function, and call that from __main__
- then you can use return as normal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With