Re: Is there any reason to call sys.exit() to close a pygame program?

Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there any reason to call sys.exit() to close a pygame program?

Sam Bull
On sab, 2017-08-12 at 18:23 +0000, skorpio wrote:
> I usually quit pygame by breaking out of the main loop and calling
> pygame.quit() followed by sys.exit(), but I can't remember anymore
> where I learned to use sys.exit and why it should be used. 

If you need to use an exit function, then I've found using the built-in
exit() function gives an error if you use pyinstaller to create a
frozen binary. That's the only reason I try to use sys.exit(). I've not
had any problems just allowing the execution to reach the end of the
file and completing naturally though.

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there any reason to call sys.exit() to close a pygame program?

Ian Mallett-2
On Sun, Aug 13, 2017 at 2:29 AM, Sam Bull <[hidden email]> wrote:
On sab, 2017-08-12 at 18:23 +0000, skorpio wrote:
> I usually quit pygame by breaking out of the main loop and calling
> pygame.quit() followed by sys.exit(), but I can't remember anymore
> where I learned to use sys.exit and why it should be used. 

​For a while (Python 2.3, 2.4) I vaguely remember `sys.exit(...)` was also useful for killing dangling PyGame windows that would persist after exceptional program termination when debugging in IDLE (i.e., put your whole program in `try`/`except`, and put `sys.exit(...)` in the `except`). This may have been where the original suggestion to use it comes from. However, that usage no longer seems to be required.
 
If you need to use an exit function, then I've found using the built-in
exit() function gives an error if you use pyinstaller to create a
frozen binary. That's the only reason I try to use sys.exit(). I've not
had any problems just allowing the execution to reach the end of the
file and completing naturally though.

​AFAIK the only current advantage of using one of the exit calls in Python is to be able to supply an exit code to the parent process. In C++, I typically return the code from `main(int,char*[])` instead because it's clearer OO encapsulation, but this isn't possible in Python.

Ian​
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Is there any reason to call sys.exit() to close a pygame program?

Thomas Benardo
In reply to this post by Sam Bull
I would suggest you to use return instead of sys.exit()

def main():
    window = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("sample")
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return
        pygame.display.update()

main()

Sent from my Windows Phone

From: [hidden email]
Sent: ‎13-‎08-‎2017 02:06 PM
To: [hidden email]
Subject: Re: [pygame] Is there any reason to call sys.exit() to close a pygame program?

On sab, 2017-08-12 at 18:23 +0000, skorpio wrote:
> I usually quit pygame by breaking out of the main loop and calling
> pygame.quit() followed by sys.exit(), but I can't remember anymore
> where I learned to use sys.exit and why it should be used. 

If you need to use an exit function, then I've found using the built-in
exit() function gives an error if you use pyinstaller to create a
frozen binary. That's the only reason I try to use sys.exit(). I've not
had any problems just allowing the execution to reach the end of the
file and completing naturally though.
Loading...