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?
If you want to know if its checked on the server side, just check if that form field exists, if request. form. get("name") gives you NULL or exception, then the checkbox should be unchecked.
prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.
To create a checkbox, first import QCheckBox from the module PyQt5. QtWidgets . Then create a new checkbox. As parameter you can set the text that is shown next to the checkbox.
POST or None) if request. method == "POST": if form. is_valid(): ... if request. POST["something_truthy"]: # Checkbox was checked ...
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: ...
x = self.folderactive.isChecked()
x
will be True
or False
—a Boolean value.
(It's the brackets at the end that make the difference.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With