名称错误: 未定义名称'quit'



我试图做一个简单的项目(tic tac toe)来使用我在Python和Tkinter中学到的东西,但是当我制作退出按钮时,它在我运行Python文件时工作,但是当我将Python文件(.py)转换为可执行文件(.exe)时发生了以下错误:

NameError: name 'quit' is not defined

这是代码:

# Importing tkinter module
from tkinter import *
# =================
while True:
    # Main screen settings
    app = Tk()
    app.title("tic tac toe")
    app.iconbitmap("icon.ico")
    app.config(bg="#00a0a0")
    # =================
    # Setting up Back Ground
    bg = PhotoImage(file="bg.png")
    canvas = Canvas(app,
                    width=500,
                    height=500,
                    bg="#00a0a0")
    canvas.grid(row=0,
                column=0)
    canvas.create_image(250, 250 , image=bg)
    # =================
    # Creating board main variable
    GameVar = [["Empty" , "Empty" , "Empty"],
            ["Empty" , "Empty" , "Empty"],
            ["Empty" , "Empty" , "Empty"]]
    # =================
    # Creating clicking functions
    xoro = "O"
    def place1():
        global xoro
        
        if GameVar[0][0] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
        
            GameVar[0][0] = xoro
            Button1.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
            
            
    def place2():
        global xoro
        
        if GameVar[0][1] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
        
            GameVar[0][1] = xoro
            Button2.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
            
            
    def place3():
        global xoro
        
        if GameVar[0][2] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
        
            GameVar[0][2] = xoro
            Button3.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
            
            
    def place4():
        global xoro
        
        if GameVar[1][0] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
        
            GameVar[1][0] = xoro
            Button4.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
            
            
    def place5():
        global xoro
        
        if GameVar[1][1] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
        
            GameVar[1][1] = xoro
            Button5.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
            
            
    def place6():
        global xoro
        
        if GameVar[1][2] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
        
            GameVar[1][2] = xoro
            Button6.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
            
            
    def place7():
        global xoro
        
        if GameVar[2][0] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
        
            GameVar[2][0] = xoro
            Button7.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
            
            
    def place8():
        global xoro
        
        if GameVar[2][1] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
        
            GameVar[2][1] = xoro
            Button8.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
            
            
    def place9():
        global xoro
        
        if GameVar[2][2] == "Empty":
            if xoro == "X":
                xoro = "O"
            elif xoro == "O":
                xoro = "X"
            
            GameVar[2][2] = xoro
            Button9.config(text=xoro,
                        font="Arial 57 bold",
                        height=1,
                        width=3)
            CheckWin()
    # =================
    # Creating function that check if any player won the game
    def CheckWin():
        global CheckXVar
        global CheckYVar
        
        CheckXVar = ((GameVar[0][0]=="X" and GameVar[0][1]=="X" and GameVar[0][2]=="X") or
                    (GameVar[1][0]=="X" and GameVar[1][1]=="X" and GameVar[1][2]=="X") or
                    (GameVar[2][0]=="X" and GameVar[2][1]=="X" and GameVar[2][2]=="X") or
                    (GameVar[0][0]=="X" and GameVar[1][0]=="X" and GameVar[2][0]=="X") or
                    (GameVar[0][1]=="X" and GameVar[1][1]=="X" and GameVar[2][1]=="X") or
                    (GameVar[0][2]=="X" and GameVar[1][2]=="X" and GameVar[2][2]=="X") or
                    (GameVar[0][0]=="X" and GameVar[1][1]=="X" and GameVar[2][2]=="X") or
                    (GameVar[2][0]=="X" and GameVar[1][1]=="X" and GameVar[0][2]=="X"))
        CheckOVar = ((GameVar[0][0]=="O" and GameVar[0][1]=="O" and GameVar[0][2]=="O") or
                    (GameVar[1][0]=="O" and GameVar[1][1]=="O" and GameVar[1][2]=="O") or
                    (GameVar[2][0]=="O" and GameVar[2][1]=="O" and GameVar[2][2]=="O") or
                    (GameVar[0][0]=="O" and GameVar[1][0]=="O" and GameVar[2][0]=="O") or
                    (GameVar[0][1]=="O" and GameVar[1][1]=="O" and GameVar[2][1]=="O") or
                    (GameVar[0][2]=="O" and GameVar[1][2]=="O" and GameVar[2][2]=="O") or
                    (GameVar[0][0]=="O" and GameVar[1][1]=="O" and GameVar[2][2]=="O") or
                    (GameVar[2][0]=="O" and GameVar[1][1]=="O" and GameVar[0][2]=="O"))
    # =================
    # Creating winning window
        if CheckXVar:
            Button1.config(state="disabled")
            Button2.config(state="disabled")
            Button3.config(state="disabled")
            Button4.config(state="disabled")
            Button5.config(state="disabled")
            Button6.config(state="disabled")
            Button7.config(state="disabled")
            Button8.config(state="disabled")
            Button9.config(state="disabled")
            
            # Setting up XWin main settings
            XWin = Toplevel(app)
            XWin.title("Winner")
            XWin.iconbitmap("icon.ico")
            # =================
            
            # Making XWin label and button
            Label(XWin,
                text="Player (X) won the game",
                fg="#c71ab0",
                font=("Arial" , 30 , "bold")).pack()
            
            Button(XWin,
                text="Click to play again",
                fg="#00a0a0",
                bd=0,
                cursor="hand2",
                relief="flat",
                font=("Arial" , 15 , "bold"),
                command=app.destroy).pack()
            # =================
        
        
        elif CheckOVar:
            Button1.config(state="disabled")
            Button2.config(state="disabled")
            Button3.config(state="disabled")
            Button4.config(state="disabled")
            Button5.config(state="disabled")
            Button6.config(state="disabled")
            Button7.config(state="disabled")
            Button8.config(state="disabled")
            Button9.config(state="disabled")
            
            # Setting up OWin main settings
            OWin = Toplevel(app)
            OWin.title("Winner")
            OWin.iconbitmap("icon.ico")
            # =================
            
            # Making OWin label and button
            Label(OWin,
                text="Player (O) won the game",
                fg="#c71ab0",
                font=("Arial" , 30 , "bold")).pack()
            
            Button(OWin,
                text="Click to play again",
                fg="#00a0a0",
                bd=0,
                cursor="hand2",
                relief="flat",
                font=("Arial" , 15 , "bold"),
                command=app.destroy).pack()
            # =================
            
    # =================
    # Creating Buttons
    Button1 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place1)
    Button1.place(x=10,
                y=10)
    Button2 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place2)
    Button2.place(x=180,
                y=10)
    Button3 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place3)
    Button3.place(x=350,
                y=10)
    
    Button4 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place4)
    Button4.place(x=10,
                y=180)
    Button5 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place5)
    Button5.place(x=180,
                y=180)
    Button6 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place6)
    Button6.place(x=350,
                y=180)
    Button7 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place7)
    Button7.place(x=10,
                y=350)
    Button8 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place8)
    Button8.place(x=180,
                y=350)
    Button9 = Button(app,
                    text="",
                    height=9,
                    width=20,
                    bg="#00a0a0",
                    fg="#c71ab0",
                    activebackground="#00a0a0",
                    activeforeground="#c71ab0",
                    bd=0,
                    command=place9)
    Button9.place(x=350,
                y=350)
    # =================
    # Creating quit function
    def quito():
        quit()
    # =================
    
    # Creating quit button
    quit_button = Button(app,
           text="Quit",
           font=("Arial" , 15 , "bold"),
           bg="#00a0a0",
           fg="#c71ab0",
           activebackground="#00a0a0",
           activeforeground="#c71ab0",
           command=quito)
    
    quit_button.grid(row=1,
                     column=0)
    # =================
    # Running main loop
    app.mainloop()
    # =================

这是基多函数:

# Creating quit function
    def quito():
        quit()
# =================

下面是创建按钮的代码:

# Creating quit button
quit_button = Button(app,
       text="Quit",
       font=("Arial" , 15 , "bold"),
       bg="#00a0a0",
       fg="#c71ab0",
       activebackground="#00a0a0",
       activeforeground="#c71ab0",
       command=quito)
quit_button.grid(row=1,
                 column=0)
# =================

;用这个command=app.destroy代替这个command=quito

quit is not defined出现是因为函数基多没有访问名为quit的变量,因此not defined。将退出按钮处的command指定为指向app.destroy像这样- command=app.destroy应该工作,因为你提供了一个应用程序的实例,因此该函数可以直接访问应用程序,允许它关闭它。

最新更新