Tkinter- checkbutton

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tuvas

    #1

    Tkinter- checkbutton

    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(c ommand=check)
    b.grid(row=0,co lumn=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

    #2
    Re: Tkinter- checkbutton

    Ere, ignore the mis-capped Checkbutton and the missed master call in
    it...

    Comment

    • Jim Segrave

      #3
      Re: Tkinter- checkbutton

      In article <1131124290.860 885.105430@g14g 2000cwa.googleg roups.com>,
      Tuvas <tuvas21@gmail. com> wrote:[color=blue]
      >Ere, ignore the mis-capped Checkbutton and the missed master call in
      >it...
      >[/color]

      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(roo t, text = 'Press Me', command = check, variable = v)
      b.grid(row = 0, column = 0)
      root.mainloop()

      --
      Jim Segrave (jes@jes-2.demon.nl)

      Comment

      • Tuvas

        #4
        Re: Tkinter- checkbutton

        That solved the problem. Thanks!

        Comment

        Working...