Tkinter- checkbutton

Discussion in 'Python' started by Tuvas, Nov 4, 2005.

  1. Tuvas

    Tuvas Guest

    I want to have a checkbutton that when it is pushed will do a function
    depending on if it was pushed before or not. Ei:

    b=checkbutton(command=check)
    b.grid(row=0,column=0)

    def check():
    if (b.value==0):
    do_stuff_here()
    elif(b.value==1)
    do_other_stuff_here()


    However, I keep running into problems with reading the data. How do I
    make this work? Thanks!
     
    Tuvas, Nov 4, 2005
    #1
    1. Advertising

  2. Tuvas

    Tuvas Guest

    Ere, ignore the mis-capped Checkbutton and the missed master call in
    it...
     
    Tuvas, Nov 4, 2005
    #2
    1. Advertising

  3. Tuvas

    Jim Segrave Guest

    In article <>,
    Tuvas <> wrote:
    >Ere, ignore the mis-capped Checkbutton and the missed master call in
    >it...
    >


    You need to use a Tkinter variable (IntVar, StringVar or whatever),
    associate it with the Checkbutton, and access it with the get() and
    set() methods:

    from Tkinter import *

    root = Tk()

    v = IntVar()
    v.set(1)

    def do_stuff_here():
    print "Value is zero"

    def do_other_stuff_here():
    print "Value is not zero"

    def check():
    if v.get() == 0:
    do_stuff_here()
    elif v.get() == 1:
    do_other_stuff_here()
    else:
    print "This is impossible, but it happened"

    b = Checkbutton(root, text = 'Press Me', command = check, variable = v)
    b.grid(row = 0, column = 0)
    root.mainloop()

    --
    Jim Segrave ()
     
    Jim Segrave, Nov 4, 2005
    #3
  4. Tuvas

    Tuvas Guest

    That solved the problem. Thanks!
     
    Tuvas, Nov 4, 2005
    #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. Elaine Jackson

    Tkinter CheckButton variables

    Elaine Jackson, Jul 1, 2004, in forum: Python
    Replies:
    1
    Views:
    448
    klappnase
    Jul 1, 2004
  2. Paul Rubin
    Replies:
    3
    Views:
    822
    Eric Brunel
    Jul 4, 2005
  3. O.R.Senthil Kumaran
    Replies:
    3
    Views:
    1,019
    Eric Brunel
    Jul 5, 2007
  4. Replies:
    0
    Views:
    363
  5. Dodo
    Replies:
    6
    Views:
    1,169
Loading...

Share This Page