使用 .destroy() 关闭弹出窗口 window 的按钮给出了弹出窗口 window 未定义的错误。使用 Python tkinter

Button to close a pop up window using .destroy() gives error that the pop-up window is not defined. Using Python tkinter

我正在尝试让我的代码生成一个弹出窗口 window 并显示某些统计信息(在 def redbutt() 中:window 名称是 newfailWindow )然后有一个按钮(用 def quit():) 编写的命令在该弹出窗口中关闭该弹出窗口。但是,即使在我使用 close window 命令声明弹出窗口 window 为全局后,它仍然说弹出窗口 window 未定义(基本上说 newfailWindow 不是定义)。 这是我的代码片段:

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

当然整个代码包括主循环的开始和结束。我只是不想在这里粘贴我的大代码哈哈

这是我从您的代码片段中提取的示例代码:

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

如果您愿意,也可以:

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