Tkinter checkbuttons and variables

Discussion in 'Python' started by Gigs_, Feb 21, 2007.

  1. Gigs_

    Gigs_ Guest

    from Tkinter import *

    states = []

    def onpress(i):
    states = not states


    root = Tk()
    for i in range(10):
    chk = Checkbutton(root, text= str(i), command=lambda i=i:
    onpress(i))
    chk.pack(side=LEFT)
    states.append(0)
    root.mainloop()
    print states

    after exiting i get everything like it suppose to but when i put command
    like this:
    command=lambda: onpress(i)
    i got only last checkbutton check.

    Why i have to pass this default argument?


    btw i have python 2.5
    thx
    Gigs_, Feb 21, 2007
    #1
    1. Advertising

  2. Gigs_ schrieb:
    > from Tkinter import *
    >
    > states = []
    >
    > def onpress(i):
    > states = not states
    >
    >
    > root = Tk()
    > for i in range(10):
    > chk = Checkbutton(root, text= str(i), command=lambda i=i: onpress(i))
    > chk.pack(side=LEFT)
    > states.append(0)
    > root.mainloop()
    > print states
    >
    > after exiting i get everything like it suppose to but when i put command
    > like this:
    > command=lambda: onpress(i)
    > i got only last checkbutton check.
    >
    > Why i have to pass this default argument?


    Because python creates a closure around the lambda that allows
    expressions inside the lambda to access surrounding variables. However,
    these variables are looked up at _runtime_, when the command is actually
    executed. Naturally, the value of i then is 9, because that's what it
    has been assigned in the last loop iteration.

    Diez
    Diez B. Roggisch, Feb 21, 2007
    #2
    1. Advertising

  3. Gigs_

    Eric Brunel Guest

    On Wed, 21 Feb 2007 15:50:57 +0100, Gigs_ <-com.hr> wrote:
    > from Tkinter import *
    >
    > states = []
    >
    > def onpress(i):
    > states = not states
    >
    >
    > root = Tk()
    > for i in range(10):
    > chk = Checkbutton(root, text= str(i), command=lambda i=i:
    > onpress(i))
    > chk.pack(side=LEFT)
    > states.append(0)
    > root.mainloop()
    > print states
    >
    > after exiting i get everything like it suppose to but when i put command
    > like this:
    > command=lambda: onpress(i)
    > i got only last checkbutton check.
    >
    > Why i have to pass this default argument?


    I'm basically not answering your question here, but the usual way to get a
    checkbuttons's state is as follows:

    states = []
    root = Tk()
    for i in range(10):
    stateVar = BooleanVar()
    chk = Checkbutton(root, text=str(i), variable=stateVar)
    chk.pack(side=LEFT)
    states.append(stateVar)
    root.mainloop()
    print [v.get() for v in states]

    If you want to get the value of one of your states, use the get() method
    on BooleanVar. If you want to change such a state, use the set(value)
    method.

    HTH
    --
    python -c "print ''.join([chr(154 - ord(c)) for c in
    'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
    Eric Brunel, Feb 21, 2007
    #3
    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. Frank Chen
    Replies:
    3
    Views:
    515
    Cameron Laird
    Oct 8, 2003
  2. William Gill

    a list of Checkbuttons

    William Gill, Aug 30, 2005, in forum: Python
    Replies:
    0
    Views:
    289
    William Gill
    Aug 30, 2005
  3. valen1260

    putting checkbuttons in a listbox

    valen1260, Dec 20, 2005, in forum: Python
    Replies:
    1
    Views:
    263
    Martin Franklin
    Dec 21, 2005
  4. Joe Van Dyk

    Tkmenubar and checkbuttons

    Joe Van Dyk, May 5, 2005, in forum: Ruby
    Replies:
    4
    Views:
    102
    Joe Van Dyk
    May 5, 2005
  5. David Douthitt
    Replies:
    8
    Views:
    270
    Hidetoshi NAGAI
    Jul 15, 2005
Loading...

Share This Page