Change Background of QLCDNumber with PyQt5 to an image

1.6k Views Asked by At

I currently have a program which changes the color of the background of the QLCDumber widget when a certain value is displayed on the lcd number, is it possible for instead of a color being the background when a certain value is reached an image appears in the background, such as a smiley face?

Here is the relevant snippet of my code responsible for the change in color.

def OnValueFound(self, value):
        self.ui.lcdNumber.display(value)
        if 100 < value < 300:
            self.ui.lcdNumber.setStyleSheet("""QLCDNumber {background-color: green; color: black;}""")
        else:
            self.ui.lcdNumber.setStyleSheet("""QLCDNumber {background-color:red; color: black;}""")

so instead of say the background color changing to another color it changes to an image?

I hope this makes sense.

1

There are 1 best solutions below

1
On BEST ANSWER

I do not know if I understood you correctly. Try it border-image: url(E:/_Qt/img/heart.png);

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; */
                                                    border-image: url(E:/_Qt/img/heart.png);
                                                    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