Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get state of Checkbutton when it's selected?

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")
like image 384
IAM Avatar asked Mar 24 '23 19:03

IAM


1 Answers

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
like image 162
Jon Clements Avatar answered Apr 01 '23 21:04

Jon Clements