Hide label when a button is clicked in Python

27,280

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

Solution 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.

Share:
27,280
deen
Author by

deen

Updated on August 02, 2020

Comments

  • deen
    deen over 3 years

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

  • deen
    deen over 9 years
    @baited- Thanks! Works fine for me. Exactly just what I need. :D