2

I have a code which reads sensor data and outputs it to a LCD number I am using python3 and pyqt5.

Now What i've been tyring to do with no luck is change the background colour of the LCD number when it reaches a certain value. e.g. when the value falls below 100 the background of the LCD widget is red or an image appears saying too low, if it is between 100-300 it is green and over 300 it goes red again. I hope this makes sense, does anyone know how I can achieve this using pyqt5?

here are the relevant segments of my code for the LCD Number

class Worker(QtCore.QThread):
    valueFound = QtCore.pyqtSignal(int, name="valueFound")

...

def run(self):
        while self.runFlag:
            self.valueFound.emit(self.Pressure_Reading())

...

self.worker = Worker(self)
self.worker.valueFound.connect(self.OnValueFound)

....

def OnValueFound(self, value):
        self.ui.lcdNumber.display(value)
Almaas Shah
  • 125
  • 2
  • 14

1 Answers1

4

Use Qt Style Sheets.

import sys
from random          import randint
from PyQt5.QtWidgets import QApplication, QMainWindow 
from PyQt5.QtCore    import QTimer

from ui import Ui_MainWindow

class Form(QMainWindow):
    def __init__(self):
        super().__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.i = 0 
        self.voltageMin = 180 
        self.voltageMax = 180
        self.ui.lcdNumberCur.display(self.i)
        self.ui.lcdNumberCur.setStyleSheet("QLCDNumber { background-color: yellow }")

        self.ui.pushButton.clicked.connect(self.startTimer)

        self.timer = QTimer(self)  
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.updateData)

        self.show()        

    def startTimer(self):  
        if self.ui.pushButton.text() == "Start Timer":
            self.timer.start(1000) 
            self.ui.pushButton.setText("Stop Timer")            
        else:
            self.ui.pushButton.setText("Start Timer")
            self.timer.stop() 

    def updateData(self):
        voltage = randint(80, 350)                  # <--- insert your average voltage here
        self.ui.lcdNumberCur.display(voltage) 
        if voltage > self.voltageMax:
            self.voltageMax = voltage
            self.ui.lcdNumberMax.display(self.voltageMax) 
            if self.voltageMax > 300:
                self.ui.lcdNumberMax.setStyleSheet("""QLCDNumber { 
                                                    background-color: red; 
                                                    color: white; }""")
            else: 
                self.ui.lcdNumberMax.setStyleSheet("""QLCDNumber 
                                                   { background-color: green; 
                                                     color: yellow;
                                                   }""")

        elif voltage < self.voltageMin:
            self.voltageMin = voltage
            self.ui.lcdNumberMin.display(self.voltageMin)  
            if self.voltageMin < 90:
                self.ui.lcdNumberMin.setStyleSheet("""QLCDNumber { 
                                                    background-color: red; 
                                                    color: white; }""")
            else: 
                self.ui.lcdNumberMin.setStyleSheet("""QLCDNumber 
                                                   { background-color: green; 
                                                     color: yellow;
                                                   }""")            


if __name__ == '__main__':
    app = QApplication(sys.argv)
    frm = Form()
    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • Thanks @S.Nick, it worked for me. Do you know if this would be possible for images too? so instead of a colour it changes the stylesheet of the LCD number to an image. – Almaas Shah Sep 13 '18 at 15:03
  • @Almaas Shah Sorry, I did not understand your question. Create a new question and attach a sample code with the problem. – S. Nick Sep 13 '18 at 15:29