Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[PyQt5] Qlabel doesn't update



  • Hello,
    I want to display measurement from my raspberry pi on my gui. Everything is working fine except it displays the value from when the script has been started and doesn't update the Label after new measurements. I'm a beginner with Python and with Qt, what am I doing wrong? Thanks for your help!

    import sys
    from PyQt5 import QtCore
    from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel
    from PyQt5.QtGui import QIcon, QPixmap
    import board
    import Adafruit_DHT
    import busio
    import adafruit_ccs811
    import time
    
    # I2C-Bus definieren
    #i2c = busio.I2C(board.SCL, board.SDA)
    
    # CCS811 (CO2-Sensor) an den I2C-Bus anbinden
    #ccs811 = adafruit_ccs811.CCS811(i2c)
    
    # Sensor Typ auswählen (DHT11), GPIO Pin auswhählen (GPIO6) und Daten in Variable schreiben
    sensor = Adafruit_DHT.DHT11
    pin = 6
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    
    
    # CCS811 (CO2-Sensor) Bereitschaft abfragen und auslesen falls bereit
    #def get_CO2():
    #    if not ccs811.data_ready:
    #        CO2 = "Nicht bereit"
    #    else if True:
    #        CO2 = ccs811.eco2
    #        CO2 = str(CO2)
    #    return(CO2)
    
    
    
    
    
    
    class App(QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.title = 'Klima- & CO2-Logger'
            self.left = 10
            self.top = 10
            self.width = 480
            self.height = 320
            self.UI()
        
        def UI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    	
            if temperature is not None:
                Temperatur1 = str(temperature)
            else:
                Temperatur1 = "...."
            if humidity is not None:
                Luftfeuchtigkeit1 = str(humidity)
            else:
                Luftfeuchtigkeit1 = "...."
    	
            label = QLabel('Temperatur', self)
            label.setStyleSheet("font: 16pt MS Shell Dlg 2")
            label.resize((131),(41))
            label.move(20,20)
            
            label2 = QLabel(self)
            Bild1 = QPixmap('Temperatur1.png')        
            label2.setPixmap(Bild1)
            label2.resize((71),(121))       
            label2.move(50,80)
            
            label3 = QLabel(self)
            label3.setText(Temperatur1 +'°C')
            label3.setStyleSheet("font: 14pt MS Shell Dlg 2")
            label3.resize((71),(41))
            label3.move(50,240)
            
            label4 = QLabel('Luftfeuchte', self)
            label4.setStyleSheet("font: 16pt MS Shell Dlg 2")
            label4.resize((121),(41))
            label4.move(180,20)
            
            label5 = QLabel(self)
            Bild2 = QPixmap('Luftfeuchte1.png')        
            label5.setPixmap(Bild2)
            label5.resize((91),(101))
            label5.move(190,90)
    		
            label6 = QLabel(self)
            label6.setText(Luftfeuchtigkeit1 +'%')
            label6.setStyleSheet("font: 14pt MS Shell Dlg 2")
            label6.resize((65),(41))
            label6.move(220,240)
    		
            label7 = QLabel('CO2-Gehalt', self)
            label7.setStyleSheet("font: 16pt MS Shell Dlg 2")
            label7.resize((121),(41))
            label7.move(340,20)
    		
            label8 = QLabel(self)
            Bild3 = QPixmap('CO2.png')        
            label8.setPixmap(Bild3)
            label8.resize((121),(71))
            label8.move(340,100)
    		
    #        label9 = QLabel(self)
    #        label.setText(get_CO2 +'ppm')
    #        label9.setStyleSheet("font: 14pt MS Shell Dlg 2")
    #        label9.resize((91),(41))
    #        label9.move(360,240)
    		
            label10 = QLabel('by Tim Kaiser', self)
            label10.move(380,295)
    		
            self.show()
    		
            time.sleep(2.0)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())
    

  • Lifetime Qt Champion

    @DrKaiser said in [PyQt5] Qlabel doesn't update:

    time.sleep(2.0)

    Why?

    In the code you posted I do not see where you're actually getting the data from the sensor?
    I suggest to use QTimer with a timeout. Connect a slot to the times timeout and in that slot call

    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    

    and set the values yout get from sensor in your labels.


  • Lifetime Qt Champion

    @DrKaiser said in [PyQt5] Qlabel doesn't update:

    time.sleep(2.0)

    Why?

    In the code you posted I do not see where you're actually getting the data from the sensor?
    I suggest to use QTimer with a timeout. Connect a slot to the times timeout and in that slot call

    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    

    and set the values yout get from sensor in your labels.



  • @jsulm Thanks for your answer. I am not sure how to implement the QTimer in my code, as a said I'm a beginner with Qt. Could you please show me an example on how to use it with my code?


  • Lifetime Qt Champion

    @DrKaiser Please start with documentation, there are examples: https://doc.qt.io/qt-5/qtimer.html


  • Lifetime Qt Champion

    @DrKaiser said in [PyQt5] Qlabel doesn't update:

    label10.move(380,295)

    Sigh. You should also read about layouts...

    Regards


  • Banned

    This is pyqt4

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())
    

    The following is the pyqt5 version

    if __name__ == '__main__':
        MainEvntThread = QApplication([])  #If you plan to use Command Line arguments look at the argparser library
    
        MainApp = App()
        MainApp.show()  # <-- Should do this here unless you need to delay it for some reason
    
        MainEvntThread .exec()
    

Log in to reply