-1

I have been quickly coding a simple QDialog with a QVBoxLayout with the functionality to enable an Arduino project of mine and I really want to update a QPushButton I have placed to make it go from 'start' to 'stop' and back every time. The QPushButton with 'start' as the text is in the main class where the QDialog has been defined and clicking it runs an associated function where the text of the button is changed to 'stop'. Now when the user clicks the button with 'stop', I want to change it back to 'start', the button should again be clickable and go to the associated function, be changed to stop and on and on. I have been unable to achieve this. Can somebody help me out?

As you can probably already tell, I'm a beginner. I have been unable to understand QThreads, processEvents(), update() and all those other stuff which popped up when I googled "updating GUI" or "refreshing GUI". Please explain in as simple and plain terms as possible, whatever I need to do!

Here is my code for just the 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_())

You can probably tell I want to be able to start and stop the stepper motor from here!

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
K.C Karthik
  • 53
  • 1
  • 7

1 Answers1

1

A very simple way to do it is creating a checkable button and connect it to the signal clicked[bool]. So every time you click the button a bool value will be passed to your slot.

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')
GiovanniPi
  • 260
  • 1
  • 8
  • Seems a reasonable solution, but the if clauses are round the wrong way in your example. – ekhumoro May 26 '15 at 16:31
  • Thank you so much for the help! It worked like a charm! Although I had to click start twice the first time I executed the program. I fixed it by setting the bool to false every time, since switching the if-else statements didn't seem to work. – K.C Karthik May 27 '15 at 14:46