4

How can I get the state of a Checkbutton in python? I have this:

def doSomething():

  if #code goes here, if checkbutton is selected
   ...

check = Checkbutton(window, text="Add both", onvalue = 1, offvalue = 0)
check.pack(side="right")
martineau
  • 119,623
  • 25
  • 170
  • 301
IAM
  • 895
  • 2
  • 13
  • 30
  • Check this post: https://stackoverflow.com/questions/4236910/getting-checkbutton-state/41081709#41081709 – Meowi Aug 02 '20 at 21:51

1 Answers1

7

You need to associate the Checkbox with a variable:

is_checked = IntVar()
check = Checkbutton(window, text="Add both", onvalue=1, offvalue=0, variable=is_checked)

Then utilise do your check such as:

if is_checked.get():
    # do something
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
Jon Clements
  • 138,671
  • 33
  • 247
  • 280