为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

阿波罗的战车 2023-12-20 16:06:17
我希望我的程序以这样的方式运行:一旦用户按下按钮,就会根据按钮的命令显示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'] = NORMALdef 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()
查看完整描述

2 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

该错误是由于这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 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()


查看完整回答
反对 回复 2023-12-20
?
holdtom

TA贡献1805条经验 获得超10个赞

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


查看完整回答
反对 回复 2023-12-20
  • 2 回答
  • 0 关注
  • 55 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信