[英]Button to close a pop up window using .destroy() gives error that the pop-up window is not defined. Using Python tkinter
I'm trying to get my code to produce a pop up window with certain stats displayed (within the def redbutt(): the window name is newfailWindow ) and then have a button (command written in def quit():)in that pop-up to close that pop-up.我试图让我的代码生成一个弹出 window 并显示某些统计信息(在 def redbutt() 内: window 名称是 newfailWindow )然后有一个按钮(用 def quit():) 编写的命令-up 关闭该弹出窗口。 However, even after I declare that pop-up window as global when I use the close window command it says that the pop-up window is not defined (basically says that newfailWindow is not defined).
但是,即使在我声明弹出 window 为全局时,当我使用 close window 命令时,它仍表示未定义弹出窗口 window (基本上说未定义 newfailWindow) Here's a snippet of my code:
这是我的代码片段:
def quit():
game_title.place(x = 350, y=1)
start_game_bt.place(x = 490, y=100)
red.place_forget()
green.place_forget()
blue.place_forget()
yellow.place_forget()
red_btn.place_forget()
green_btn.place_forget()
blue_btn.place_forget()
yellow_btn.place_forget()
home_btn.place_forget()
level_label.place_forget()
start_btn_final.place_forget()
players_name.delete(0,tk.END)
user.config(text = "Player Name: ")
user.place_forget()
score_label.place_forget()
next_level_btn.place_forget()
user_name="none"
global color_list
global color_list_OG
global score
global level
level = 1
level_label.config(text = "Level: "+str(level))
score=0
color_list = []
color_list_OG = []
for x in range(3):
randomWord = random.choice(colors)
color_list.append(randomWord)
color_list_OG.append(randomWord)
global newfailWindow
newfailWindow.destroy()
def redbutt():
global score
global color_list_OG
global color_list
global newfailWindow
for c in range(len(color_list)):
if color_list[c]=="red":
score = score+1
color_list.pop(c)
score_label.config(text= "Score: "+str(score))
break
if color_list[c]=="green":
newfailWindow = tk.Toplevel(main_window)
newfailWindow.title("Loser")
newfailWindow.geometry('250x125')
newfailWindow.configure(bg = 'orangered')
positionRight = int(newfailWindow.winfo_screenwidth()/2 - 250/2)
positionDown = int(newfailWindow.winfo_screenheight()/3 - 125/2)
newfailWindow.geometry("+{}+{}".format(positionRight, positionDown))
Fail = tk.Label(newfailWindow, text ="You failed!!!!", font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
scoreFail = tk.Label(newfailWindow, text ="Score: "+str(score), font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
backFail = tk.Button(newfailWindow, text = "Home", command = quit, font = ("Franklin gothic heavy", 20), bg = 'blue', fg = 'yellow').pack()
break
if color_list[c]=="yellow":
newfailWindow = tk.Toplevel(main_window)
newfailWindow.title("Loser")
newfailWindow.geometry('250x125')
newfailWindow.configure(bg = 'orangered')
positionRight = int(newfailWindow.winfo_screenwidth()/2 - 250/2)
positionDown = int(newfailWindow.winfo_screenheight()/3 - 125/2)
newfailWindow.geometry("+{}+{}".format(positionRight, positionDown))
Fail = tk.Label(newfailWindow, text ="You failed!!!!", font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
scoreFail = tk.Label(newfailWindow, text ="Score: "+str(score), font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
backFail = tk.Button(newfailWindow, text = "Home", command = quit, font = ("Franklin gothic heavy", 20), bg = 'blue', fg = 'yellow').pack()
break
if color_list[c]=="blue":
newfailWindow = tk.Toplevel(main_window)
newfailWindow.title("Loser")
newfailWindow.geometry('250x125')
newfailWindow.configure(bg = 'orangered')
positionRight = int(newfailWindow.winfo_screenwidth()/2 - 250/2)
positionDown = int(newfailWindow.winfo_screenheight()/3 - 125/2)
newfailWindow.geometry("+{}+{}".format(positionRight, positionDown))
Fail = tk.Label(newfailWindow, text ="You failed!!!!", font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
scoreFail = tk.Label(newfailWindow, text ="Score: "+str(score), font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
backFail = tk.Button(newfailWindow, text = "Home", command = quit, font = ("Franklin gothic heavy", 20), bg = 'blue', fg = 'yellow').pack()
break
Of course the whole code includes the mainloop start and end.当然整个代码包括主循环开始和结束。 I just didn't want to paste my large code here lol
我只是不想在这里粘贴我的大代码哈哈
Here is a sample code that I took from your code snippet:这是我从您的代码片段中获取的示例代码:
import tkinter as tk
def _quit(newfailWindow):
newfailWindow.destroy()
def redbutt():
newfailWindow = tk.Toplevel(top)
Fail = tk.Label(newfailWindow, text ="You failed!!!!", font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
#scoreFail = tk.Label(newfailWindow, text ="Score: "+str(score), font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
backFail = tk.Button(newfailWindow, text = "Home", command = lambda:_quit(newfailWindow), font = ("Franklin gothic heavy", 20), bg = 'blue', fg = 'yellow').pack()
top = tk.Tk()
top.title('Eric\'s Archiver')
tk.Button(top, text='press', command=redbutt).pack()
top.mainloop()
also if you want it your way:如果你想要你的方式:
import tkinter as tk
newfailWindow = None
def _quit():
global newfailWindow
if newfailWindow:
newfailWindow.destroy()
#newfailWindow = None
def redbutt():
global newfailWindow
newfailWindow = tk.Toplevel(top)
Fail = tk.Label(newfailWindow, text ="You failed!!!!", font = ("Franklin gothic heavy", 20), bg = 'orangered', fg = 'black').pack()
backFail = tk.Button(newfailWindow, text = "Home", command = _quit, font = ("Franklin gothic heavy", 20), bg = 'blue', fg = 'yellow').pack()
top = tk.Tk()
top.title('Eric\'s Archiver')
tk.Button(top, text='press', command=redbutt).pack()
top.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.