在 PyQt4 中检查 QPushButton 的状态?

lea*_*ode 3 python pyqt4 qpushbutton

我想写一个if条件作为QPushButton. if如果按钮已启用,我想执行。那么,我如何检查按钮的状态。

代码:

self.pushButton = QtGui.QPushButton()
self.pushbutton.setEnabled(False)
Run Code Online (Sandbox Code Playgroud)
self.pushbutton.setEnabled(False) ##some where in the another class I have enabled it this line will not be executed all the time,so I want the check state.
Run Code Online (Sandbox Code Playgroud)
if self.pushbutton.checkstate() == Enabled:  ##??? How to write this condition?
    some code I want to execute
Run Code Online (Sandbox Code Playgroud)

Oli*_*ver 6

使用QPushButton.isEnabled. 以下代码在启用和禁用之间切换按钮,并使用“if”测试根据状态执行某些操作。它适用于 PyQt5,但您只需要调整导入即可使其与 PyQt4 一起使用(如果您使用的是 Python 2.x,您还必须调整打印语句):

from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QPushButton

def check_button():
    if push.isEnabled():
        print('enabled')
        push.setEnabled(False)
    else:
        print('not enabled')
        push.setEnabled(True)

    QTimer.singleShot(1000, check_button)

app = QApplication([])
push = QPushButton()
push.show()
QTimer.singleShot(0, check_button)
app.exec()
Run Code Online (Sandbox Code Playgroud)