NameError: name 'quit' is not defined
NameError: name 'quit' is not defined
我试图做一个简单的项目(井字游戏)来使用我在 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()
# =================
这是 quito 函数:
# 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)
# =================
TLDR;使用此 command=app.destroy
而不是此 command=quito
.
quit is not defined
的出现是因为函数 quito 无法访问名为 quit
的变量,因此无法访问名为 not defined
的变量。
将退出按钮处的 command
指定为指向 app.destroy
像这样 - command=app.destroy
应该可以工作,因为你提供了一个应用程序实例,因此该函数可以直接访问应用程序,允许它关闭它。
我试图做一个简单的项目(井字游戏)来使用我在 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()
# =================
这是 quito 函数:
# 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)
# =================
TLDR;使用此 command=app.destroy
而不是此 command=quito
.
quit is not defined
的出现是因为函数 quito 无法访问名为 quit
的变量,因此无法访问名为 not defined
的变量。
将退出按钮处的 command
指定为指向 app.destroy
像这样 - command=app.destroy
应该可以工作,因为你提供了一个应用程序实例,因此该函数可以直接访问应用程序,允许它关闭它。