Tkinter toggle a Label Widget based on checkbutton value

Discussion in 'Python' started by O.R.Senthil Kumaran, Jul 4, 2007.

  1. 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
     
    O.R.Senthil Kumaran, Jul 4, 2007
    #1
    1. Advertising

  2. O.R.Senthil Kumaran wrote:
    > 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()
     
    Wojciech =?iso-8859-2?Q?Mu=B3a?=, Jul 4, 2007
    #2
    1. Advertising

  3. * Wojciech Mu?a <> [2007-07-04 20:13:06]:

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



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


    --
    O.R.Senthil Kumaran
    http://uthcode.sarovar.org
     
    O.R.Senthil Kumaran, Jul 5, 2007
    #3
  4. O.R.Senthil Kumaran

    Eric Brunel Guest

    On Wed, 04 Jul 2007 21:51:34 +0200, O.R.Senthil Kumaran
    <> 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)

    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
    --
    python -c "print ''.join([chr(154 - ord(c)) for c in
    'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
     
    Eric Brunel, Jul 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    346
  2. Frederic Rentsch
    Replies:
    0
    Views:
    251
    Frederic Rentsch
    Jul 8, 2012
  3. Terry Reedy
    Replies:
    15
    Views:
    640
    Frederic Rentsch
    Jul 16, 2012
  4. Frederic Rentsch
    Replies:
    0
    Views:
    433
    Frederic Rentsch
    Jul 9, 2012
  5. Knut Krueger
    Replies:
    8
    Views:
    282
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page