2012-03-15 10 views
0

このシンプルなカウントダウンタイマーのフォームをここではhttps://github.com/lunaryorn/snippets/blob/master/qt4/countdown.pyに変更しました。今私はカウントダウンタイマーの背後に背景画像を持っていたい。どのようにこれを行うにはどのようなアイデア?pyqt4 qlabelの背景画像

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

答えて

2

CSSの背景として画像を入れてください:

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

をまた、ちょうどQLabelsのQPixmapのプロパティを編集します。 – ely

+0

@EMS:どうしたらいいですか?私が見ることのできる唯一の選択肢は、カスタムの 'paintEvent'です。 – Avaris

+0

QLabelのsetPixmap()は自動的にそれを発生させるはずです。タイマーのように表示されるようにpaintEvent()をオーバーライドする必要があるかもしれません... PyQTで画像をペイントする一般的な方法であり、考慮すべき有用なアプローチであるため、これを単に言及する必要があると感じました。しかし、この特定の作業には他のアプローチが適しているかもしれません。 – ely