如何检查是否在pyqt中选中了复选框 [英] How to check if a checkbox is checked in pyqt

查看:308
本文介绍了如何检查是否在pyqt中选中了复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据复选框是否被选中来做一个条件语句.我尝试过类似以下的操作,但它总是返回 true.

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?

推荐答案

self.folderactive.isChecked 不是布尔值,它是一种方法 - 在布尔上下文中,它总是计算为正确.如果您想要复选框的状态,只需调用该方法:

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:
    ...

这篇关于如何检查是否在pyqt中选中了复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆