pyqt4 qlabel中的背景图片

时间:2012-03-15 20:46:16

标签: python css pyqt4

我在这里修改了这个简单的倒数计时器表格:https://github.com/lunaryorn/snippets/blob/master/qt4/countdown.py如下。现在我想在倒数计时器后面有一个背景图片。任何想法如何做到这一点?

#!/usr/bin/python

import sys

from PyQt4.QtCore import Qt, QTime, QTimer
from PyQt4.QtGui import QApplication, QLabel, QPixmap



class CountdownWidget(QLabel):
    def __init__(self, countdown, parent=None):
        QLabel.__init__(self, parent)
        self.countdown = countdown
        self.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter);
        #self.setPixmap(QPixmap('/tmp/test.png'));
        self.setStyleSheet("QLabel {font-size : 400px; color : blue; }");
        self.setText(str(self.countdown))
        # setup the countdown timer
        self.timer = QTimer(self)
        self.timer.timeout.connect(self._update_time)

    def start(self):
        # update the display every second
        self.timer.start(1000)

    def _update_time(self):
        # this gets called every seconds
        # adjust the remaining time
        #self.countdown = self.countdown.addSecs(-1)
        self.countdown = self.countdown -1
        # if the remaining time reached zero, stop the timer
        if self.countdown <= 0:
            self.timer.stop()
        # update the display
        self.setText(str(self.countdown))
        #self.setPixmap(QPixmap('/tmp/test.png')); #<--- doesn't work


def main():
    app = QApplication(sys.argv)
    widget = CountdownWidget(30)
    widget.start()
    widget.show()
    app.exec_()


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

将图像作为CSS背景:

self.setStyleSheet("QLabel {font-size : 400px; color : blue; background-image: url('tmp/test.jpg');}");