Tkinter toggle a Label Widget based on checkbutton value

O.R.Senthil Kumaran orsenthil at users.sourceforge.net
Wed Jul 4 21:51:34 CEST 2007


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


-- 
O.R.Senthil Kumaran
http://uthcode.sarovar.org



More information about the Python-list mailing list