Home > Back-end >  NameError: name 'quit' is not defined
NameError: name 'quit' is not defined

Time:04-17

I was trying to make a simple project (tic tac toe) to use the things I learned in python and tkinter, But when I was making a quit button it worked when I run the python file, but when I converted the python file(.py) to an executable file(.exe) an error happened it was: (NameError: name 'quit' is not defined).

This is the code:

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

This is the quito function:

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

And this is the code that make button:

# 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)
# =================

I wanted to put images but I and because I don't have enough points in stack overflow

Please any one help.

CodePudding user response:

TLDR; use this command=app.destroy instead of this command=quito.

quit is not defined occurs because the function quito doesn't have access to such variable named quit hence not defined. Assign the command at the quit button to point to app.destroy like so - command=app.destroy that supposed to work because you provide an instance of the app hence the function has a direct access to the app allowing it to close it.

  • Related