【发布时间】:2021-03-22 12:25:22
【问题描述】:
我正在尝试让我的代码生成一个显示某些统计信息的弹出窗口(在 def redbutt() 中:窗口名称是 newfailWindow ),然后有一个按钮(用 def quit() 编写的命令:)在该弹出窗口中关闭该弹出窗口。但是,即使在我使用关闭窗口命令时将该弹出窗口声明为全局窗口后,它仍说未定义弹出窗口(基本上说未定义 newfailWindow)。 这是我的代码的 sn-p:
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
当然,整个代码包括主循环的开始和结束。我只是不想在这里粘贴我的大代码大声笑
【问题讨论】:
-
是的,现在很乱。我需要删除重复的 if 语句。我应该解释得更好吗?
-
显示更多代码。我无法复制它。
-
ok 试试这个
backFail = tk.Button(newfailWindow, text = "Home", command = lambda : quit(newfailWindow), font = ("Franklin gothic heavy", 20), bg = 'blue', fg = 'yellow').pack()也添加 newfailWindow 作为参数退出和删除全局
标签: python python-3.x python-2.7 tkinter