Tkinter toggle a Label Widget based on checkbutton value

Eric Brunel see.signature at no.spam
Thu Jul 5 10:43:07 CEST 2007

On Wed, 04 Jul 2007 21:51:34 +0200, O.R.Senthil Kumaran  
<orsenthil at> wrote:
> Following is a tk code, which will display a checkbutton, and when  
> checkbox is
> enabled, it will show the below present Label.
> What I was trying is, when checkbox is enabled the Label should be shown  
> and
> when checkbox is disabled, the window should look like before.
> But, I am finding that once enabled (shown), its not  
> very-straightforward to
> hide it from the window.
> Any suggestions on  how can i make this checkbutton effect.
> 1) Press Enable IP, the Label IP should be shown.
> 2) Toggle Enable IP (So that its unset). the Label IP should not be  
> shown.
> #!/usr/bin/python
> from Tkinter import *
> root = Tk()
> root.title('something')
> x = StringVar()
> def display():
>     if x.get():
>         ip = Label(root,text="IP:")
>         ip.grid(row=3,column=0)
> proxy = Checkbutton(root, text="Enable IP:", variable =  
> x,onvalue="proxy",
>         offvalue="",command=display)
> proxy.grid(row=2,column=0)
> root.mainloop()

Wojciech gave a perfectly valid answer that'll work in all cases.

Here is another one, that you may or may not be able to use, which  
consists in using the same Tkinter variable for your check-button's value  
and your label's text:
 from Tkinter import *

root = Tk()

labelString = StringVar()

b = Checkbutton(root, text="Enable IP:", variable=labelString,
                       onvalue='IP:', offvalue='')
b.grid(row=2, column=0)

Label(root, textvariable=labelString).grid(row=3, column=0)


This doesn't really make the label "disappear" when the check-button is  
off, but simply sets its text to the empty string.

python -c "print ''.join([chr(154 - ord(c)) for c in  

More information about the Python-list mailing list