Reputation: 870
Im running a Pygame window using a GUI. When for example, a user clicks on a button on the GUI, the Pygame window appears. However, when I want to quit the Pygame window, my GUI quits too. Im sure this is beacuse of the following lines of code:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
sys.exit() exits everything, which is why the IDE closes with the Pygame window. But how do I only close the Pygae Window? Ive tried this:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
break
But this doesnt work. Any suggestions?
The GUI im using Pyqt4 with Python 3.
Upvotes: 0
Views: 2210
Reputation: 101042
I guess that your code snippet
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
break
is part of your main loop, running in an while
loop, like
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
break
The problem is that break
will exit the for
loop, not the while
loop, eventually leading to an exception since you exit pygame but are probably trying to draw an the screen etc. etc.
A simple fix is to use a variable as condition for your while
loop, like
quit = False
while not quit:
for event in pygame.event.get():
if event.type==pygame.QUIT:
quit = True
...
or make sure to exit the while
loop
while True:
if pygame.event.get(pygame.QUIT): # only check for QUIT event
break
for event in pygame.event.get():
...
Upvotes: 2