What is the difference between .quit and .QUIT in pygame
QUIT
is the enumerator constant for an event type (see event
module). The quit event occurs when the pygame window is closed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# [...]
quit()
is a function which uninitialize all pygame modules. This function should be called at the end of the applicaiition:
# initialize all imported pygame modules
pygame.init()
# application loop
while True:
# [...]
# uninitialize all pygame modules
pygame.quit()
Difference between .quit and .destroy on tkinter
.quit()
causes mainloop
to exit, but doesn't directly cause any widgets to be destroyed. However, if there's no code after calling mainloop
then the script exits, and all widgets will be destroyed.
.destroy()
will destroy a widget. If you destroy the root window then all other widgets will be destroyed and mainloop
will stop.
is there a force quit option for pygame programs?
Here you compare the event type to the predefined quit
function of python. This is obviously never evaluted to True
.
Do instead if event.type == pygame.QUIT:
.
What is the difference between root.destroy() and root.quit()?
quit()
stops the TCL interpreter. This is in most cases what you want, because your Tkinter-app will also stop. It can be a problem, if you e.g. call your app from idle. idle is itself a Tkinker-app, so if you callquit()
in your app and the TCL interpreter gets terminated, idle will also terminate (or get confused ).
destroy()
just terminates the mainloop and deletes all widgets. So it seems to be safer if you call your app from another Tkinter app, or if you have multiple mainloops."
taken from http://www.daniweb.com/forums/thread66698.html
Python exit commands - why so many and when should each be used?
The functions* quit()
, exit()
, and sys.exit()
function in the same way: they raise the SystemExit
exception. So there is no real difference, except that sys.exit()
is always available but exit()
and quit()
are only available if the site
module is imported (docs).
The os._exit()
function is special, it exits immediately without calling any cleanup functions (it doesn't flush buffers, for example). This is designed for highly specialized use cases... basically, only in the child after an os.fork()
call.
Conclusion
Use
exit()
orquit()
in the REPL.Use
sys.exit()
in scripts, orraise SystemExit()
if you prefer.Use
os._exit()
for child processes to exit after a call toos.fork()
.
All of these can be called without arguments, or you can specify the exit status, e.g., exit(1)
or raise SystemExit(1)
to exit with status 1. Note that portable programs are limited to exit status codes in the range 0-255, if you raise SystemExit(256)
on many systems this will get truncated and your process will actually exit with status 0.
Footnotes
* Actually, quit()
and exit()
are callable instance objects, but I think it's okay to call them functions.
pygame error: video system has not initialized
Your culprit is this line:
if event.type == pygame.quit():
Instead of checking for the QUIT
event, you're calling the pygame.quit()
function the first time your program enters the game loop, and the function uninitializes your pygame
state, which then causes your game to crash the next time you try to access any pygame
state.
What you want is to check if you got the pygame.QUIT
event.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
running = True
while running:
for event in pygame.event.get():
# this should be the event pygame.QUIT
# and not the function pygame.quit()
if event.type == pygame.QUIT:
running = False
Related Topics
What Is the Most Efficient Way to Get First and Last Line of a Text File
2D List Has Weird Behavor When Trying to Modify a Single Value
Python Selenium Chrome Webdriver
How to Select Python Version in Pycharm
How to Find Out If Python Is Compiled with Ucs-2 or Ucs-4
Creating a Bat File for Python Script
Extract Number from String in Python
What Is the Pythonic Way to Unpack Tuples
Failed to Get Convolution Algorithm. This Is Probably Because Cudnn Failed to Initialize,
Encode and Assemble Multiple Features in Pyspark
How to Capture Output of Python's Interpreter and Show in a Text Widget
In Django - Model Inheritance - Does It Allow You to Override a Parent Model's Attribute
Check If All Elements of a List Are of the Same Type
Class That Acts as Mapping for **Unpacking
Get Previous Row's Value and Calculate New Column Pandas Python