Home>

On the Rasberry Pi, place the LCD Number using Qt Desinger
We are working on creating a GUI application like the one shown below with Python 3.5.3.

I want to do the following two things.
・ After pressing the START button, the LCD Number value is updated every arbitrary time.
・ Stop with STOP button

Currently
I created the following code,
It does not work as intended.

Since I've just started studying python, it's not enough to study,
I'm sorry, can you give me some advice?

#-*-coding: utf-8-*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.11.2
#
# WARNING! All changes made in this file will be lost!
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog (object):
    def setupUi (self, Dialog):
        Dialog.setObjectName ("Dialog")
        Dialog.resize (400, 300)
        font = QtGui.QFont ()
        font.setPointSize (14)
        Dialog.setFont (font)
        self.buttonBox = QtWidgets.QDialogButtonBox (Dialog)
        self.buttonBox.setGeometry (QtCore.QRect (30, 240, 341, 32))
        self.buttonBox.setOrientation (QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons (QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName ("buttonBox")
        self.lcdNumber = QtWidgets.QLCDNumber (Dialog)
        self.lcdNumber.setGeometry (QtCore.QRect (90, 60, 91, 51))
        self.lcdNumber.setAutoFillBackground (True)
        self.lcdNumber.setObjectName ("lcdNumber")
        self.pushButton = QtWidgets.QPushButton (Dialog)
        self.pushButton.setGeometry (QtCore.QRect (230, 70, 100, 40))
        self.pushButton.setObjectName ("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton (Dialog)
        self.pushButton_2.setGeometry (QtCore.QRect (230, 130, 100, 40))
        self.pushButton_2.setObjectName ("pushButton_2")
        self.actionOnStart = QtWidgets.QAction (Dialog)
        self.actionOnStart.setObjectName ("actionOnStart")
        self.actionOnStop = QtWidgets.QAction (Dialog)
        self.actionOnStop.setObjectName ("actionOnStop")
        self.retranslateUi (Dialog)
        self.buttonBox.accepted.connect (Dialog.accept)
        self.buttonBox.rejected.connect (Dialog.reject)
        self.pushButton.clicked.connect (self.OnStart)
        self.pushButton_2.clicked.connect (self.OnStop)
        QtCore.QMetaObject.connectSlotsByName (Dialog)
    def retranslateUi (self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle (_translate ("Dialog", "Dialog"))
        self.pushButton.setText (_translate ("Dialog", "START"))
        self.pushButton_2.setText (_translate ("Dialog", "STOP"))
        self.actionOnStart.setText (_translate ("Dialog", "OnStart"))
        self.actionOnStop.setText (_translate ("Dialog", "OnStop"))
    def OnStart (self):
        i = 0
        while True:
            i = i + 1
            self.lcdNumber.display (i)
            time.sleep (1)
    def OnStop (self):
        print ('I don't know the code after pressing the Stop button')

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication (sys.argv)
    Dialog = QtWidgets.QDialog ()
    ui = Ui_Dialog ()
    ui.setupUi (Dialog)
    Dialog.show ()
    sys.exit (app.exec _ ())
  • Answer # 1

    If you write a loop, the process will stop there, so use a timer.

    def __init __ (self):
        self._timer = QtCore.QTimer ()
        self._timer.timeout.connect (self.run)
    def on_start (self):
        self._timer.start (1000)
    def run (self):
        self._lcd_number.display (
            self._lcd_number.intValue () + 1
        )
        self._timer.start (1000)
    def on_stop (self):
        self._timer.stop ()

    The name of the object is changed.