简体   繁体   中英

Background picture in pyqt4 qlabel

I modified this simple countdown timer form here: https://github.com/lunaryorn/snippets/blob/master/qt4/countdown.py as follows. Now I want to have a background image behind the countdown timer. Any ideas how to do this?

#!/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()

将图像作为CSS背景:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM