如何使用按钮命令删除 Tkinter 标签?

我希望我的程序以这样的方式运行:一旦用户按下按钮,就会根据按钮的命令显示Info一个名为的标签。在相同的条件下(如果按下按钮),我想添加一个删除/销毁.GameInfoLabelInfoInfoBackGameInfoLabel

我试图在下面的代码中实现这一点,但我收到了消息

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


阿波罗的战车
浏览 59回答 2
2回答

哔哔one

该错误是由于这GameInfoLabel是内部的局部变量GameInfo()并且在内部不可访问QuitGameInfo()。您可以通过声明GameInfoLabel为全局或将其传递给QuitGameInfo()via 参数来修复此错误。同样适用于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,显示框架。单击按钮时Back,隐藏框架。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 frameButton(root, text="Info", command=GameInfo, width="20", height="3").pack()# create the game info frame but don't show it initiallyinfo_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()

holdtom

GameInfoLabel该方法是本地的GameInfo()。这意味着一旦该方法完成运行,其中声明的任何内容都将不再存在。通常的解决方案是将变量传递/返回到函数以获取结果,例如您的游戏信息可以返回标签,但是由于您希望在事件发生时自动调用这些函数,例如按下按钮,所以这并不那么容易。我相信解决您的问题的最简单的解决方案是GameInfoLabel全局声明变量(在全局范围内),这并不总是最好的编码实践,但我不确定 tkinter 是否能够将变量参数传递给事件处理程序,这可以变得复杂。另外,正如 acw1668 所提到的,您可以在从初始化返回的新标签上立即调用 .pack() Label(...)。然后打包不会返还标签,因此我们单独进行返还。这应该有效,请仔细阅读。from tkinter import * root = Tk() root.title("Game Menu") root.geometry("1920x1080")  root.resizable(True, True) # Declare any global UI ComponentsGameInfoLabel = None # Dont set a Label yetdef QuitGameInfo():    GameInfoLabel.destroy()    BackInfoButton['state'] = NORMALdef 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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python