Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 937
Joshua Strot Avatar asked Dec 08 '13 16:12

Joshua Strot


People also ask

How do I know if a checkbox is checked in flask?

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.

Which method is used to check the status of checkbox?

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.

How do I add a checkbox in PYQT?

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.

How do you check checkbox is checked or not in Django?

POST or None) if request. method == "POST": if form. is_valid(): ... if request. POST["something_truthy"]: # Checkbox was checked ...


2 Answers

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:     ... 
like image 196
mata Avatar answered Sep 19 '22 00:09

mata


x = self.folderactive.isChecked() 

x will be True or False—a Boolean value.

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

like image 24
Terry Avatar answered Sep 18 '22 00:09

Terry