Hide label when a button is clicked in Python

时光总嘲笑我的痴心妄想 提交于 2019-12-11 05:59:18

问题


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


回答1:


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

edit (working example)

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



回答2:


Use can use grid_remove() to hide the label. like self.myLabel.grid_remove(). If you want to show it again then use self.myLabel.grid(). This will show widget on its original position on grid.



来源:https://stackoverflow.com/questions/27654494/hide-label-when-a-button-is-clicked-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!