So I am trying to use Tkinter to receive a text input, and then run pygames from that to do an animation. I get an error though when I close Pygames.
A simplified version of how I plan to use Pygames:
def the_program():
if spot.get().strip() == "":
tkMessageBox.showerror("X", "Y")
else:
code = spot.get().strip()
pygame.init()
pygame.display.set_caption('X')
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Running Tkinter:
root = Tk()
frame = Frame(root)
text = Label(frame, text='X')
spot = Entry(frame)
button = Button(frame, text = 'Ready?', command = the_program) "Starts Pygames"
frame.pack()
text.pack()
spot.pack()
button.pack()
root.mainloop()
Pygames opens up fine and runs well, but when I close it I get this error:
Traceback (most recent call last):
File "C:\Python26\Practice\legit Battle Master.py", line 82, in <module>
root.mainloop()
File "C:\Python26\lib\lib-tk\Tkinter.py", line 1017, in mainloop
self.tk.mainloop(n)
File "C:\Python26\lib\lib-tk\Tkinter.py", 开发者_如何学Cline 1412, in __call__
raise SystemExit, msg
How can I avoid this? I tried removing "sys.exit()", but python crashes.
You are trying to exit from the pygame mainloop with sys.exit()
, which exits the whole running application, including the tkinter GUI you started before pygame. You should exit the pygame mainloop (the only while
clause) with a conditional. e.g:
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
running = False
...
精彩评论