【发布时间】:2020-10-22 08:55:36
【问题描述】:
我希望我的程序以这样一种方式运行,一旦用户按下 Info 按钮,由于来自 Info 的命令,会显示一个名为 GameInfoLabel 的标签按钮。在相同的条件下(如果按下 Info 按钮),我想添加一个 Back 按钮来删除/销毁GameInfoLabel。
我试图在下面的代码中实现这一点,但我收到了消息
NameError:名称“GameInfoLabel”未定义。
from tkinter import *
root = Tk()
root.title("Game Menu")
root.geometry("1920x1080")
root.resizable(True, True)
def QuitGameInfo():
GameInfoLabel.destroy()
BackInfoButton['state'] = NORMAL
def GameInfo():
RulesNotepad = open("GameInfo.txt",'r')
Rules = RulesNotepad.read()
GameInfoLabel = Label(root, text = Rules, fg = "blue", bg = "red", height = "14", width = "140").pack()
BackInfoButton = Button(root, text = "Back", command = QuitGameInfo).pack()
RulesNotepad.close()
button3 = Button(root, text = "Info", command = GameInfo, width = "20", height = "3").pack()
root.mainloop()
【问题讨论】:
-
您的代码中存在一些问题:1)
GameInfoLabel是GameInfo()内部的局部变量,因此无法在QuitGameInfo()内部访问。这会导致错误; 2)GameInfoLabel是None,因为它是Label(...).pack()的结果; 3)GameInfoButton里面QuitGameInfo()没有定义。 -
为什么将
BackInfoButton['state']更改为NORMAL,因为它已经处于NORMAL状态? -
我不知道为什么我现在回过头来看这行。
标签: python python-3.x user-interface tkinter python-3.7