Joshua Strot
Joshua Strot

Reputation: 2521

How to check if a checkbox is checked in pyqt

I'm trying to make a conditional statement based on whether a checkbox is checked or not. I've tried something like the following, but it always returns as true.

self.folderactive = QtGui.QCheckBox(self.folders)
self.folderactive.setGeometry(QtCore.QRect(50, 390, 71, 21))
self.folderactive.setObjectName(_fromUtf8("folderactive"))
if self.folderactive.isChecked:
    folders.createDir('Desktop')
    print "pass"
elif not self.folderactive.isChecked:
    folders.deleteDir('Desktop')
    print "nopass"

Is there a way to get a bool value of whether a checkbox is checked or not?

Upvotes: 35

Views: 99176

Answers (2)

Terry
Terry

Reputation: 41

x = self.folderactive.isChecked()

x will be True or False—a Boolean value.

(It's the brackets at the end that make the difference.)

Upvotes: 4

mata
mata

Reputation: 69012

self.folderactive.isChecked isn't a boolean, it's a method - which, in a boolean context, will always evaluate to True. If you want the state of the checkbox, just invoke the method:

if self.folderactive.isChecked():
    ...
else:
    ...

Upvotes: 59

Related Questions