如何使用按钮命令删除 Tkinter 标签?
How do I delete a Tkinter label using a button command?
我希望我的程序 运行 的方式是,一旦用户按下 Info 按钮,就会显示一个名为 GameInfoLabel
的标签,因为来自 Info 按钮的命令。在相同的条件下(如果 Info 按钮被按下),我想添加一个 Back 按钮 deletes/destroys GameInfoLabel
.
我试图在下面的代码中实现它,但我收到了消息
NameError: name 'GameInfoLabel' is not defined.
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()
如 acw1668 的评论所述,GameInfoLabel
是 GameInfo()
方法的局部变量。这意味着一旦此方法完成 运行 其中声明的任何内容都不复存在。
通常的解决方案是 passing/returning 函数的变量以获得结果,例如您的游戏信息可以 return 标签,但是由于您希望在事件发生时自动调用这些函数,例如你的按钮被按下了,这可没那么简单
我相信解决您问题的最简单方法是全局声明 GameInfoLabel
变量(在全局范围内),这并不总是最佳编码实践,但我不确定 tkinter 的能力将变量参数传递给事件处理程序,这可能很复杂。
另外,如 acw1668 所述,您会立即在新标签上调用 .pack(),该标签是 return 从初始化 Label(...)
编辑的。然后 Pack 没有 return 标签,所以我们单独做。
这应该可以,请仔细阅读。
from tkinter import *
root = Tk()
root.title("Game Menu")
root.geometry("1920x1080")
root.resizable(True, True)
# Declare any global UI Components
GameInfoLabel = None # Dont set a Label yet
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")
GameInfoLabel.pack()
BackInfoButton = Button(root, text = "Back", command = QuitGameInfo).pack()
RulesNotepad.close()
button3 = Button(root, text = "Info", command = GameInfo, width = "20", height = "3")
button3.pack()
root.mainloop()
错误是由于 GameInfoLabel
是 GameInfo()
内部的局部变量,在 QuitGameInfo()
.
内部不可访问
您可以通过将 GameInfoLabel
声明为全局或通过参数将其传递给 QuitGameInfo()
来修复此错误。同样适用于 BackInfoButton
。
但是您需要解决另一个问题:GameInfoLabel
和 BackInfoButton
都是 None
因为它们是 pack()
.
的结果
下面是使用全局解决方案修改后的代码:
from tkinter import *
root = Tk()
root.title("Game Menu")
root.geometry("1920x1080")
root.resizable(True, True)
def QuitGameInfo():
GameInfoLabel.destroy()
#BackInfoButton['state'] = NORMAL # why ??? Should it be destroyed as well?
BackInfoButton.destroy()
def GameInfo():
global GameInfoLabel, BackInfoButton
with open("GameInfo.txt",'r') as RulesNotepad:
Rules = RulesNotepad.read()
GameInfoLabel = Label(root, text = Rules, fg = "blue", bg = "red", height = "14", width = "140")
GameInfoLabel.pack()
BackInfoButton = Button(root, text = "Back", command = QuitGameInfo)
BackInfoButton.pack()
Button(root, text = "Info", command = GameInfo, width = "20", height = "3").pack()
root.mainloop()
但是我建议使用一个框架来容纳 GameInfoLabel
和 BackInfoButton
并且框架最初是隐藏的。单击 Info 按钮时,显示框架。单击 后退 按钮时,隐藏框架。
from tkinter import *
root = Tk()
root.title("Game Menu")
root.geometry("1920x1080")
root.resizable(True, True)
def GameInfo():
with open("GameInfo.txt",'r') as RulesNotepad:
Rules = RulesNotepad.read()
GameInfoLabel.config(text=Rules)
info_frame.pack() # show the game info frame
Button(root, text="Info", command=GameInfo, width="20", height="3").pack()
# create the game info frame but don't show it initially
info_frame = Frame(root)
GameInfoLabel = Label(info_frame, fg="blue", bg="red", height="14", width="140")
GameInfoLabel.pack()
Button(info_frame, text="Back", command=info_frame.pack_forget).pack()
root.mainloop()
我希望我的程序 运行 的方式是,一旦用户按下 Info 按钮,就会显示一个名为 GameInfoLabel
的标签,因为来自 Info 按钮的命令。在相同的条件下(如果 Info 按钮被按下),我想添加一个 Back 按钮 deletes/destroys GameInfoLabel
.
我试图在下面的代码中实现它,但我收到了消息
NameError: name 'GameInfoLabel' is not defined.
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()
如 acw1668 的评论所述,GameInfoLabel
是 GameInfo()
方法的局部变量。这意味着一旦此方法完成 运行 其中声明的任何内容都不复存在。
通常的解决方案是 passing/returning 函数的变量以获得结果,例如您的游戏信息可以 return 标签,但是由于您希望在事件发生时自动调用这些函数,例如你的按钮被按下了,这可没那么简单
我相信解决您问题的最简单方法是全局声明 GameInfoLabel
变量(在全局范围内),这并不总是最佳编码实践,但我不确定 tkinter 的能力将变量参数传递给事件处理程序,这可能很复杂。
另外,如 acw1668 所述,您会立即在新标签上调用 .pack(),该标签是 return 从初始化 Label(...)
编辑的。然后 Pack 没有 return 标签,所以我们单独做。
这应该可以,请仔细阅读。
from tkinter import *
root = Tk()
root.title("Game Menu")
root.geometry("1920x1080")
root.resizable(True, True)
# Declare any global UI Components
GameInfoLabel = None # Dont set a Label yet
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")
GameInfoLabel.pack()
BackInfoButton = Button(root, text = "Back", command = QuitGameInfo).pack()
RulesNotepad.close()
button3 = Button(root, text = "Info", command = GameInfo, width = "20", height = "3")
button3.pack()
root.mainloop()
错误是由于 GameInfoLabel
是 GameInfo()
内部的局部变量,在 QuitGameInfo()
.
您可以通过将 GameInfoLabel
声明为全局或通过参数将其传递给 QuitGameInfo()
来修复此错误。同样适用于 BackInfoButton
。
但是您需要解决另一个问题:GameInfoLabel
和 BackInfoButton
都是 None
因为它们是 pack()
.
下面是使用全局解决方案修改后的代码:
from tkinter import *
root = Tk()
root.title("Game Menu")
root.geometry("1920x1080")
root.resizable(True, True)
def QuitGameInfo():
GameInfoLabel.destroy()
#BackInfoButton['state'] = NORMAL # why ??? Should it be destroyed as well?
BackInfoButton.destroy()
def GameInfo():
global GameInfoLabel, BackInfoButton
with open("GameInfo.txt",'r') as RulesNotepad:
Rules = RulesNotepad.read()
GameInfoLabel = Label(root, text = Rules, fg = "blue", bg = "red", height = "14", width = "140")
GameInfoLabel.pack()
BackInfoButton = Button(root, text = "Back", command = QuitGameInfo)
BackInfoButton.pack()
Button(root, text = "Info", command = GameInfo, width = "20", height = "3").pack()
root.mainloop()
但是我建议使用一个框架来容纳 GameInfoLabel
和 BackInfoButton
并且框架最初是隐藏的。单击 Info 按钮时,显示框架。单击 后退 按钮时,隐藏框架。
from tkinter import *
root = Tk()
root.title("Game Menu")
root.geometry("1920x1080")
root.resizable(True, True)
def GameInfo():
with open("GameInfo.txt",'r') as RulesNotepad:
Rules = RulesNotepad.read()
GameInfoLabel.config(text=Rules)
info_frame.pack() # show the game info frame
Button(root, text="Info", command=GameInfo, width="20", height="3").pack()
# create the game info frame but don't show it initially
info_frame = Frame(root)
GameInfoLabel = Label(info_frame, fg="blue", bg="red", height="14", width="140")
GameInfoLabel.pack()
Button(info_frame, text="Back", command=info_frame.pack_forget).pack()
root.mainloop()