如何在QPushbutton的两个功能之间来回切换?

时间:2015-05-24 12:10:43

标签: qt python-2.7 pyqt4 qpushbutton

我一直在用QVBoxLayout快速编写一个简单的QDialog,其功能是启用我的Arduino项目,我真的想要更新我已经放置的QPushButton,让它从'start'到'stop'并返回每个时间。带有'start'作为文本的QPushButton位于已定义QDialog的主类中,单击它会运行一个关联的函数,其中按钮的文本更改为'stop'。现在,当用户单击“停止”按钮时,我想将其更改回“开始”,按钮应再次可点击并转到相关功能,更改为停止和打开等。我一直无法做到这一点。有人可以帮助我吗?

你可能已经知道了,我是初学者。我一直无法理解QThreads,processEvents(),update()以及当我用Google搜索“更新GUI”或“刷新GUI”时弹出的所有其他内容。请尽可能简单明了地解释,无论我需要做什么!

以下是我的GUI代码:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class StepperMotor(QDialog):   
    def __init__(self):
        QDialog.__init__(self)
        layout1 = QVBoxLayout()
        label1 = QLabel("Stepper Motor Control")
        label1.setAlignment(Qt.AlignCenter)
        angle = QLineEdit()
        speed = QLineEdit()
        self.period = QLineEdit()
        angle.setPlaceholderText("Enter Angle")
        speed.setPlaceholderText("Enter Speed in RPM")
        layout1.addWidget(label1)
        layout1.addWidget(angle)
        layout1.addWidget(speed)
        layout2 = QHBoxLayout()        
        layout3 = QHBoxLayout()
        layout4 = QHBoxLayout()
        layout5 = QHBoxLayout()
        labelx = QLabel("Type")
        labelx.setAlignment(Qt.AlignCenter)
        self.cb1 = QCheckBox()
        self.cb1.setText("Linear")
        layout2.addWidget(self.cb1)
        self.cb2 = QCheckBox()
        self.cb2.setText("Sinusoidal")
        layout2.addWidget(self.cb2)
        self.cb3 = QCheckBox()
        self.cb3.setText("Randomized")
        layout2.addWidget(self.cb3)
        layout2.setAlignment(Qt.AlignCenter)
        self.labely = QLabel("Direction")
        self.labely.setAlignment(Qt.AlignCenter)
        self.labely.setEnabled(False)
        self.cb4 = QRadioButton()
        self.cb4.setText("Clockwise")
        self.cb5 = QRadioButton()
        self.cb5.setText("Counter-Clockwise")
        layout3.setAlignment(Qt.AlignCenter)        
        layout3.addWidget(self.cb4)        
        layout3.addWidget(self.cb5)
        self.group = QButtonGroup()
        self.group.addButton(self.cb4)
        self.group.addButton(self.cb5) 
        self.cb4.setEnabled(False)
        self.cb5.setEnabled(False)
        self.btn1 = QPushButton("Start")
        #self.btn1.clicked.connect(self.start)
        btn2 = QPushButton("Cancel")
        btn2.clicked.connect(self.close)
        layout1.addWidget(labelx)
        layout1.addLayout(layout2)
        layout1.addWidget(self.labely)
        layout1.addLayout(layout3)
        layout1.addLayout(layout4)
        layout1.addLayout(layout5)
        layout1.addWidget(label2)
        layout4.addWidget(self.btn1)
        layout4.addWidget(btn2)              
        layout5.addWidget(label3)
        layout5.addWidget(label4)
        self.setLayout(layout1)
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.setWindowTitle("SMCM")
        self.setFocus()
        self.cb1.stateChanged.connect(self.statuscheck1)
        self.cb2.stateChanged.connect(self.statuscheck2)
        self.cb3.stateChanged.connect(self.statuscheck3)

    def statuscheck1(self):
        if self.cb1.isChecked():
            self.cb2.setEnabled(False)
            self.cb3.setEnabled(False)            
            self.cb4.setEnabled(True)
            self.cb5.setEnabled(True)
            self.labely.setEnabled(True)
        elif self.cb2.isChecked() or self.cb3.isChecked():
            self.cb4.setChecked(False)
            self.cb5.setChecked(False)
            self.cb4.setEnabled(False)
            self.cb5.setEnabled(False)
            self.labely.setEnabled(False)
        else:
            self.group.setExclusive(False)        
            self.cb4.setChecked(False)
            self.cb5.setChecked(False)
            self.group.setExclusive(True)
            self.cb2.setEnabled(True)
            self.cb3.setEnabled(True)
            self.cb4.setEnabled(False)
            self.cb5.setEnabled(False)
            self.labely.setEnabled(False)

    def statuscheck2(self):
        if self.cb2.isChecked():
            self.cb1.setEnabled(False)
            self.cb3.setEnabled(False)
        else:
            self.cb1.setEnabled(True)
            self.cb3.setEnabled(True)

    def statuscheck3(self):
        if self.cb3.isChecked():
            self.cb1.setEnabled(False)
            self.cb2.setEnabled(False)    
        else:
            self.cb1.setEnabled(True)
            self.cb2.setEnabled(True)

    def event(self, event): 
        if event.type() == QEvent.EnterWhatsThisMode:
            QWhatsThis.leaveWhatsThisMode()
            return True
        return QDialog.event(self, event)

if __name__ == "__main__":

    app = QApplication(sys.argv)
    dialog = StepperMotor()
    dialog.show()
    sys.exit(app.exec_())

您可以告诉我希望能够从这里启动和停止步进电机!

1 个答案:

答案 0 :(得分:1)

一种非常简单的方法是创建一个可检查按钮并将其连接到单击[bool]的信号。因此,每次单击该按钮时,bool值都将传递到您的插槽。

self.btn = QtGui.QPushButton('Stop')
self.btn.clicked[bool].connect(self.onStop)
self.btn.setCheckable(True)

def onStop(self, status):
    if status:
        #your code when stop is pressed
        self.btn.setText('Start')
    else:
        #your code when start is pressed
        self.btn.setText('Stop')