在Python中单击按钮时隐藏标签 [英] Hide label when a button is clicked in Python

查看:1868
本文介绍了在Python中单击按钮时隐藏标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python(Tkinter)中单击按钮时,如何隐藏现有的Label?

How could I hide an existing Label when a button is clicked in Python(Tkinter)?

推荐答案

这实际上取决于您使用的几何管理器.如果您使用

This really depends on the geometry manager you used. If you use

lbl = Tkinter.Label(parent)

要创建标签,您将使用以下方法之一将其隐藏.

to create the label, you will use one of the following to hide it.

lbl.grid_forget()
lbl.pack_forget()
lbl.place_forget()

编辑(工作示例)

import tkinter

class MyClass(tkinter.Frame):
    def __init__(self,parent, *args, **kwargs):
        tkinter.Frame.__init__(self, parent, *args, **kwargs)

        self.btn = tkinter.Button(self,text='Don\'t push me',command=self.buttonCmd)
        self.btn.grid(row=0,column=0,sticky='nwes')
        self.lbl = tkinter.Label(self,text='Push it, it\'s fun')
        self.lbl.grid(row=0,column=1,sticky='nwes')

    def buttonCmd(self,*args,**kwargs):
        self.lbl.grid_forget()

root = tkinter.Tk()
MyFrame = MyClass(root)
MyFrame.pack(expand='true',fill='both')
root.mainloop()

这篇关于在Python中单击按钮时隐藏标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆