Python 我如何在不关闭tkinter的情况下关闭pygames?
所以我尝试使用Tkinter来接收文本输入,然后从中运行pygames来制作动画。但我在关闭Pygames时出错 我计划如何使用Pygames的简化版本:Python 我如何在不关闭tkinter的情况下关闭pygames?,python,tkinter,pygame,sys,Python,Tkinter,Pygame,Sys,所以我尝试使用Tkinter来接收文本输入,然后从中运行pygames来制作动画。但我在关闭Pygames时出错 我计划如何使用Pygames的简化版本: def the_program(): if spot.get().strip() == "": tkMessageBox.showerror("X", "Y") else: code = spot.get().strip() pygame.init() pygam
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()
运行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打开良好,运行良好,但当我关闭它时,会出现以下错误:
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", line 1412, in __call__
raise SystemExit, msg
我怎样才能避免这种情况?我试图删除sys.exit,但python崩溃了 您正试图使用sys.exit退出pygame主循环,该循环退出整个正在运行的应用程序,包括您在pygame之前启动的tkinter GUI。您应该使用条件语句退出pygame main循环的only while子句。e、 g:
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
running = False
...
请选择所有源代码行并按CTRL+K以创建更好的语法高亮显示和代码格式:与我删除sys.exit时发生的情况相同。将打开一个崩溃窗口,显示pythonw.exe遇到问题等。可能您必须在Tkinter程序终止时调用pygame.quit。崩溃是由我的程序中的另一个问题引起的,我已修复该问题。感谢您的帮助,它现在可以工作了。实际上,我在运行=False之后添加了一个空返回