Tkinter toggle a Label Widget based on checkbutton value

  • Thread starter O.R.Senthil Kumaran
  • Start date
O

O.R.Senthil Kumaran

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

Wojciech =?iso-8859-2?Q?Mu=B3a?=

O.R.Senthil Kumaran said:
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()

ip = Label(root,text="IP:")
def display(): global ip
if x.get():
#ip = Label(root,text="IP:")
ip.grid(row=3,column=0)
else:
ip.grid_forget()
 
O

O.R.Senthil Kumaran

* Wojciech Mu?a said:
ip = Label(root,text="IP:")
else:
ip.grid_forget()


Thanks a lot, that perfectly does the trick. :) Did not know about
grid_forget() earlier.
 
E

Eric Brunel

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)

root.mainloop()
--------------------------------------------------

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

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top