用PyQt5将QLCDNumber的背景更改为图像

时间:2018-09-13 15:44:11

标签: python-3.x qt user-interface pyqt5 qlcdnumber

我目前有一个程序,当在LCD编号上显示某个值时,该程序会更改QLCDumber小部件的背景颜色,当达到某个值时,是否有可能将颜色显示为背景而不是背景色在背景中,例如笑脸?

这是我的代码中与颜色变化有关的摘要。

options

而不是说背景色变成另一种颜色,而是变成图像?

我希望这是有道理的。

1 个答案:

答案 0 :(得分:1)

我不知道我是否正确理解了你。试试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