1

I have written a programme which can control a PWM output on a raspberry pi from the command line but I would now like to control it using a GUI.

I am designing the GUI using PyQt5 to control a PWM output, I would like to retrieve the value from the Label which is adjusted with a dial to then be used to Change the duty cycle of my PWM output.

I am using a raspberry pi 3 Model B+.

I have attached the relevant section of my code below:

Setting up of the Main Window - standard ui to py conversion that happens when using pyuic.

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
    self.verticalLayout.setObjectName("verticalLayout")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setAlignment(QtCore.Qt.AlignCenter)
    self.label.setObjectName("label")
    self.verticalLayout.addWidget(self.label)
    self.dial = QtWidgets.QDial(self.centralwidget)
    self.dial.setObjectName("dial")
    self.verticalLayout.addWidget(self.dial)
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    self.dial.valueChanged['int'].connect(self.label.setNum)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

Function which is trying to retrieve the value from the label and ultimately update the duty cycle.

    def ChangeSpeed(self):
        self.My_PWM.start(0)
        while True:
            if 0 < self.ui.label:
                self.My_PWM.ChangeDutyCycle(self.ui.label)
            elif self.ui.label<0:
                self.My_PWM.ChangeDutyCycle(0)
                print("Programme Ended")
                break

I get a type error: unorderable tyoes int()

This question is not just straight forward conversion as it does not allow be to convert he QLabel Value to an integer.

Thanks!

Almaas Shah
  • 125
  • 2
  • 14
  • You have to convert (cast) the text from the label to an `int` first before comparison. See https://stackoverflow.com/questions/14886881/unorderable-types-int-str for more info. – TrebledJ Nov 13 '18 at 16:39
  • I have tried that and i get an error which states "int() argument must be a string, a bytes like object or a number, not 'QLabel'". – Almaas Shah Nov 13 '18 at 16:43
  • 1
    change `self.ui.label` to `int(self.ui.label.text())` in the `ChangeSpeed` method – S. Nick Nov 13 '18 at 16:44
  • I literally just tried both of those , but gave me another error. "ValueError invalid literal for int() with base 10: 'TextLabel'" – Almaas Shah Nov 13 '18 at 16:48
  • Seems like `self.ui.label.text()` is returning "TextLabel". Then the cast to `int` fails. Maybe provide a check against this? Input validation? – TrebledJ Nov 13 '18 at 16:52
  • Possible duplicate of [Short way to convert string to int Python 3](https://stackoverflow.com/questions/41837247/short-way-to-convert-string-to-int-python-3) – eyllanesc Nov 13 '18 at 16:53
  • 1
    I do not think it is a duplicate, The conventional method of converting a string to an int does not seem to be working hence why i posted this question. – Almaas Shah Nov 13 '18 at 16:56
  • @TrebuchetMS it seems as this line which comes after the mainwindow section is affecting is self.label.setText(_translate("MainWindow","TextLabel")) any idea how I can change this? – Almaas Shah Nov 13 '18 at 17:01

1 Answers1

0

Ok to resolve this issue, I had to change the code of the label from "self.label.setText..." to "self.label.setNum(0)", this set the starting value as zero and then standard integer conversion was carried out using int(self.ui.label.text()). I hope this makes sense

The relevant sections of the code were as follows:

  def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setNum(0)
        self.pushButton.setText(_translate("MainWindow", "Set Speed"))

I have since added a push Button To set the duty cycle.

and this was the conversion part of my code:

def ChangeSpeed(self):
        self.My_PWM.start(0)
        if 0 < int(self.ui.label.text()):
            self.My_PWM.ChangeDutyCycle(int(self.ui.label.text()))
        elif int(self.ui.label.text())<0:
            self.My_PWM.ChangeDutyCycle(0)
            print("Programme Ended")

I hope this makes sense for those of you who wish to convert a Qlabel to an int you must be wary that if you use QtDesigner as I did and carry out a pyuic5 conversion it will automatically set any value in your label to a text value or an empty string and you simply have to convert this to a integer using setNum.

Almaas Shah
  • 125
  • 2
  • 14