簡體   English   中英

pyqt4 qlabel中的背景圖片

[英]Background picture in pyqt4 qlabel

我在這里修改了這個簡單的倒數計時器表格: 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()

將圖像作為CSS背景:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM