0

そこで、Tkinterを使用してテキスト入力を受け取り、そこからpygamesを実行してアニメーションを作成しようとしています。Pygamesを閉じるとエラーが発生します。

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()

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がクラッシュします。

4

1 に答える 1

0

でpygamemainloopを終了しようとしてsys.exit()います。これは、pygameの前に開始したtkinter GUIを含む、実行中のアプリケーション全体を終了します。条件付きでpygameメインループ(唯一のwhile句)を終了する必要があります。例えば:

running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            running = False
...
于 2011-05-12T20:20:47.560 に答える