Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. [PyQt5] Qlabel doesn't update
Forum Updated to NodeBB v4.3 + New Features

[PyQt5] Qlabel doesn't update

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 4 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DrKaiser
    wrote on last edited by
    #1

    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_())
    
    jsulmJ aha_1980A 2 Replies Last reply
    0
    • D DrKaiser

      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_())
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      3
      • jsulmJ jsulm

        @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.

        D Offline
        D Offline
        DrKaiser
        wrote on last edited by
        #3

        @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?

        jsulmJ 1 Reply Last reply
        0
        • D DrKaiser

          @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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • D DrKaiser

            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_())
            
            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            label10.move(380,295)

            Sigh. You should also read about layouts...

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved