如何解决python3 Tic Tac Toe Game中的“ _tkinter.TclError:错误的窗口路径名”错误

时间:2019-06-28 19:25:34

标签: python python-3.x tkinter

我正在尝试使用python 3.7和tkinter创建一个Tic Tac Toe游戏。

第一局游戏结束后,当我返回主菜单并尝试再次启动它时,出现此错误

  

与_destroy函数相关的“ _tkinter.TclError:错误的窗口路径名”错误。

我该如何解决?

这是我的代码:

import tkinter as tk

LARGE_FONT = ("Verdana", 16)
BUTTON_FONT = ("Comic Sans MS", 30, "bold")

frames_list = []
btn_list = []
turn = "X"


class TicTacToe(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(Main)

    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


class Main(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="Tic Tac Toe", font=LARGE_FONT).pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Options",
                  command=lambda: master.switch_frame(Options)).pack()
        tk.Button(self, text="Start game", command=lambda: master.switch_frame(Game)).pack()
        tk.Button(self, text="Exit", command=lambda: master.destroy()).pack()


class Options(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="Options", font=LARGE_FONT).grid(row=0, columnspan=3)
        tk.Button(self, text="Return to start page",
                  command=lambda: master.switch_frame(Main)).grid(row=11, columnspan=3)


class Game(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="Tic Tac Toe", font=LARGE_FONT).grid(row=0, column=0, columnspan=3)
        self.board()
        tk.Button(self, text="Return to start page",
                  command=lambda: master.switch_frame(Main)).grid(row=4, column=0, columnspan=3)

    def change_turn(self):
        global turn
        if turn == "O":
            turn = "X"
        elif turn == "X":
            turn = "O"

    def check_win(self):

        if btn_list[0]["text"] == btn_list[1]["text"] == btn_list[2]["text"] == "X" or btn_list[0]["text"] == \
                btn_list[1]["text"] == btn_list[2]["text"] == "O":
            print("{} wins".format(btn_list[0]["text"]))
        elif btn_list[3]["text"] == btn_list[4]["text"] == btn_list[5]["text"] == "X" or btn_list[3]["text"] == \
                btn_list[4]["text"] == btn_list[5]["text"] == "O":
            print("{} wins".format(btn_list[3]["text"]))
        elif btn_list[6]["text"] == btn_list[7]["text"] == btn_list[8]["text"] == "X" or btn_list[6]["text"] == \
                btn_list[7]["text"] == btn_list[8]["text"] == "O":
            print("{} wins".format(btn_list[6]["text"]))

        elif btn_list[0]["text"] == btn_list[3]["text"] == btn_list[6]["text"] == "X" or btn_list[0]["text"] == \
                btn_list[3]["text"] == btn_list[6]["text"] == "O":
            print("{} wins".format(btn_list[0]["text"]))
        elif btn_list[1]["text"] == btn_list[4]["text"] == btn_list[7]["text"] == "X" or btn_list[1]["text"] == \
                btn_list[4]["text"] == btn_list[7]["text"] == "O":
            print("{} wins".format(btn_list[1]["text"]))
        elif btn_list[2]["text"] == btn_list[5]["text"] == btn_list[8]["text"] == "X" or btn_list[2]["text"] == \
                btn_list[5]["text"] == btn_list[8]["text"] == "O":
            print("{} wins".format(btn_list[2]["text"]))

        elif btn_list[0]["text"] == btn_list[4]["text"] == btn_list[8]["text"] == "X" or btn_list[0]["text"] == \
                btn_list[4]["text"] == btn_list[8]["text"] == "O":
            print("{} wins".format(btn_list[0]["text"]))
        elif btn_list[2]["text"] == btn_list[4]["text"] == btn_list[6]["text"] == "X" or btn_list[2]["text"] == \
                btn_list[4]["text"] == btn_list[6]["text"] == "O":
            print("{} wins".format(btn_list[2]["text"]))
        else:
            self.change_turn()

    def process_turn(self, index):
        btn_list[index].config(text=turn, state="disabled")
        self.check_win()

    def board(self):
        index = 0
        for i in range(3):
            i += 1
            for x in range(3):
                frames_list.append(tk.Frame(self, width=80, height=80))
                frames_list[index].propagate(False)
                frames_list[index].grid(row=i, column=x, sticky="nsew", padx=2, pady=2)
                btn_list.append(tk.Button(frames_list[index], font=BUTTON_FONT,
                                          command=lambda index=index: self.process_turn(index)))
                btn_list[index].pack(expand=True, fill="both")
                x += 1
                index += 1
            i += 1


if __name__ == "__main__":
    app = TicTacToe()
    app.title("Tic Tac Toe")
    app.geometry("300x320+10+10")
    app.mainloop()

0 个答案:

没有答案