2016-12-28 8 views
0

QPushButtonという状態のifを書きたいと思います。ボタンが有効な場合はifを実行したいと思います。では、ボタンの状態を確認するにはどうすればいいですか?PyQt4のQPushButtonの状態を確認しますか?

コード:

self.pushButton = QtGui.QPushButton() 
self.pushbutton.setEnabled(False) 

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. 

if self.pushbutton.checkstate() == Enabled: ##??? How to write this condition? 
    some code I want to execute 

答えて

1

使用QPushButton.isEnabled。次のコードは、ボタンを有効と無効の間で切り替え、 "if"テストを使用して状態に応じて何かを行います。 PyQt5用ですが、Python 2.xを使用している場合は、PyQt4での使用に合わせてインポートを調整するだけで済みます。

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() 
関連する問題