无法获得PyQt5Designer LCD盒以接收显示信号并更新显示

时间:2019-01-29 18:52:27

标签: multithreading signals pyqt5 slots

我在寻找如何使线程将整数值发送到PyQT中的“ LCD”显示器的解决方案后感到非常困惑。以下是我的代码,该代码采用ADC的值并采用adafruit GPS模块来收集一些数据。我想弄清楚如何将线程中的整数值发送到LCD显示器(在本例中为MphLCD显示器),以便可以在GUI中显示实时值。尝试一切后,我的代码可能一团糟,但是当我认为得到它时,应用程序因从其他线程或其他内容访问GUI项目而崩溃。我对QT相当陌生,因此尽管设计人员会帮助我进行设置,但它只是做到了,所以我不理解“表单”或何时,何时完成以及何时插入新定义

import busio
import sys
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
import time
import serial
import adafruit_gps
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

#-----------------------------------------------------------------
# Initialize MCP3008 ADC
#-----------------------------------------------------------------
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D5)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# create an analog input channel on pin 0
chan = AnalogIn(mcp, MCP.P0)

#print('Raw ADC Value: ', chan.value)
#print('ADC Voltage: ' + str(chan.voltage) + 'V')

#----------------------------------------------------------------
# Initialize GPS
#----------------------------------------------------------------
# Create a serial connection for the GPS connection using default speed and
# a slightly higher timeout (GPS modules typically update once a second).
# Default speed requires timeout of 3000, twice/second timeout of 1500
uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1500)

# Create a GPS module instance.
gps = adafruit_gps.GPS(uart, debug=False)

# Turn on the basic GGA and RMC info (what you typically want)
gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')

# Set update rate to twice/second (2hz)
gps.send_command(b'PMTK220,2000')

#-------------------------------------------------------------
# Qt Stuff Here
#--------------------------------------------------------------
class Ui_Form(object):
    Speed = pyqtSignal(int)


    def setupUi(self, Form):



        Form.setObjectName("Form")
        Form.resize(800, 480)
        Form.setStyleSheet("background-image: url(:/roccopic/VCSciroccoscaled.png);")
        def setlcd(QGraphicsObject):
            self.MphLCD = QtWidgets.QLCDNumber(setupUi.Form)
            self.MphLCD.setGeometry(QtCore.QRect(600, 230, 71, 61))
            self.MphLCD.setStyleSheet("background-image: url(:/roccopic/black.png);\n"
"border-image: url(:/roccopic/black.png);")
            self.MphLCD.setObjectName("MphLCD")
            self.Speed.connect(self.MphLCD.display)
        self.TempBar = QtWidgets.QProgressBar(Form)
        self.TempBar.setGeometry(QtCore.QRect(130, 410, 101, 20))
        self.TempBar.setAutoFillBackground(False)
        self.TempBar.setStyleSheet("")
        self.TempBar.setMinimum(135)
        self.TempBar.setMaximum(185)
        self.TempBar.setProperty("value", 150)
        self.TempBar.setTextVisible(False)
        self.TempBar.setOrientation(QtCore.Qt.Horizontal)
        self.TempBar.setObjectName("TempBar")
        self.FuelBar = QtWidgets.QProgressBar(Form)
        self.FuelBar.setGeometry(QtCore.QRect(570, 411, 101, 20))
        self.FuelBar.setProperty("value", 24)
        self.FuelBar.setTextVisible(False)
        self.FuelBar.setOrientation(QtCore.Qt.Horizontal)
        self.FuelBar.setObjectName("FuelBar")
        self.RpmLCD = QtWidgets.QLCDNumber(Form)
        self.RpmLCD.setGeometry(QtCore.QRect(130, 230, 71, 61))
        self.RpmLCD.setStyleSheet("background-image: url(:/roccopic/black.png);\n"
"border-image: url(:/roccopic/black.png);")
        self.RpmLCD.setObjectName("RpmLCD")
        #self.RpmLCD.display(99)
        self.DateLCD = QtWidgets.QLCDNumber(Form)
        self.DateLCD.setGeometry(QtCore.QRect(310, 120, 181, 23))
        self.DateLCD.setStyleSheet("border-image: url(:/roccopic/black.png);\n"
"background-image: url(:/roccopic/black.png);")
        self.DateLCD.setObjectName("DateLCD")
        #self.DateLCD.display(9999)
        self.MilesLCD = QtWidgets.QLCDNumber(Form)
        self.MilesLCD.setGeometry(QtCore.QRect(310, 350, 181, 16))
        self.MilesLCD.setStyleSheet("border-image: url(:/roccopic/black.png);\n"
"background-image: url(:/roccopic/black.png);")
        self.MilesLCD.setObjectName("MilesLCD")
        #self.MilesLCD.display(int(gps.speed_knots))
        self.TimeLCD = QtWidgets.QLCDNumber(Form)
        self.TimeLCD.setGeometry(QtCore.QRect(310, 370, 181, 16))
        self.TimeLCD.setStyleSheet("border-image: url(:/roccopic/black.png);\n"
"background-image: url(:/roccopic/black.png);")
        self.TimeLCD.setObjectName("TimeLCD")
        #self.TimeLCD.display(time)


        self.workerThread = WorkerThread()
        self.workerThread.start()




        self.retranslateUi(Form)
        Form.showFullScreen()
        QtCore.QMetaObject.connectSlotsByName(Form)


    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "SCIROCCO"))


class WorkerThread(QThread):

    def __init__(self, parent=None):
        super(WorkerThread, self).__init__(parent)

    def run(QGraphicsObject):
        print("Done with thread")
        last_print = time.monotonic()
        while True:
            gps.update()
            current = time.monotonic()
            if current - last_print >= 1:
                last_print = current
            if not gps.has_fix:
                # Try again if we don't have a fix yet.
                print('Waiting for fix...')
                continue
            # We have a fix! (gps.has_fix is true)
            # Print out details about the fix like location, date, etc.
            print('=' * 40)  # Print a separator line.
            print('Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}'.format(
                gps.timestamp_utc.tm_mon,   # Grab parts of the time from the
                gps.timestamp_utc.tm_mday,  # struct_time object that holds
                gps.timestamp_utc.tm_year,  # the fix time.  Note you might
                gps.timestamp_utc.tm_hour,  # not get all data like year, day,
                gps.timestamp_utc.tm_min,   # month!
                gps.timestamp_utc.tm_sec))
            print('Latitude: {0:.6f} degrees'.format(gps.latitude))
            print('Longitude: {0:.6f} degrees'.format(gps.longitude))
            print('Fix quality: {}'.format(gps.fix_quality))
            # Some attributes beyond latitude, longitude and timestamp are optional
            # and might not be present.  Check if they're None before trying to use!
            if gps.satellites is not None:
                print('# satellites: {}'.format(gps.satellites))
            if gps.altitude_m is not None:
                print('Altitude: {} meters'.format(gps.altitude_m))
            if gps.track_angle_deg is not None:
                self.Speed.emit(gps.speed_knots)#print('Speed: {} MPH'.format(gps.speed_knots*1.150779))
            if gps.track_angle_deg is not None:
                print('Track angle: {} degrees'.format(gps.track_angle_deg))
            if gps.horizontal_dilution is not None:
                print('Horizontal dilution: {}'.format(gps.horizontal_dilution))
            if gps.height_geoid is not None:
                print('Height geo ID: {} meters'.format(gps.height_geoid))


import scirocco_rc

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *

class WorkerThread(QThread):

    Speed = pyqtSignal(int)                                     # <---

    def __init__(self, parent=None):
        super().__init__()
        self.num  = 0
        self.flag = True

    def run(self):
        while self.flag:
            # ...
            # Here we get the integer values for the LCD
            self.num += 1
            QThread.msleep(2000)      # imitate the work flow
            self.Speed.emit(self.num)                           # <---

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(800, 480)

        self.MphLCD = QLCDNumber(Form)
        self.MphLCD.setGeometry(QRect(600, 230, 71, 61))
        self.MphLCD.setObjectName("MphLCD")

        self.workerThread = WorkerThread()
        self.workerThread.Speed.connect(self.MphLCD.display)    # <--- 
        self.workerThread.start()

        self.retranslateUi(Form)
#        Form.showFullScreen()
        QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "SCIROCCO"))  

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    Form = QWidget()
    Form.resize(800, 480)
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

enter image description here