在 QLabel 中显示 .gif 动画 [英] Showing a .gif animation in QLabel
本文介绍了在 QLabel 中显示 .gif 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 QLabel
小部件中与文本一起显示 .gif 动画.
I would like to show a .gif animation in a QLabel
widget, alongside text.
以下代码不起作用:
self.status_txt = QtGui.QLabel('Loading... <img src="etc/loading.gif">')
因为图像不会动画.
我尝试使用 QMovie
对象来实现它:
I tried achiving it by using a QMovie
object:
self.status_txt = QtGui.QLabel("Loading...")
movie = QtGui.QMovie("etc/loading.gif")
self.status_txt.setMovie(movie)
movie.start()
但是我不能把动画和文字放在一起.除了使用两个不同的标签之外,还有其他解决方案吗?
But then I can't put the animation and the text together. Is there a different solution besides using two different labels?
推荐答案
您可以在标签上添加一个 Layout,然后添加另一个带有文本的标签...
you can add a Layout to the label, and then add another Label with the text to that...
self.status_txt = QtGui.QLabel()
movie = QtGui.QMovie("etc/loading.gif")
self.status_txt.setMovie(movie)
movie.start()
self.status_txt.setLayout(QtGui.QHBoxLayout())
self.status_txt.layout().addWidget(QLabel('Loading...'))
编辑:
如果您使用自己版本的 QLabel 和 QPainter 来自己绘制文本,则是可能的:
it's possible if you use your own version of a QLabel and a QPainter to paint the text yourself:
from PyQt4.QtCore import QSize
from PyQt4.QtGui import QApplication, QLabel, QMovie, QPainter, QFontMetrics
class QTextMovieLabel(QLabel):
def __init__(self, text, fileName):
QLabel.__init__(self)
self._text = text
m = QMovie(fileName)
m.start()
self.setMovie(m)
def setMovie(self, movie):
QLabel.setMovie(self, movie)
s=movie.currentImage().size()
self._movieWidth = s.width()
self._movieHeight = s.height()
def paintEvent(self, evt):
QLabel.paintEvent(self, evt)
p = QPainter(self)
p.setFont(self.font())
x = self._movieWidth + 6
y = (self.height() + p.fontMetrics().xHeight()) / 2
p.drawText(x, y, self._text)
p.end()
def sizeHint(self):
fm = QFontMetrics(self.font())
return QSize(self._movieWidth + 6 + fm.width(self._text),
self._movieHeight)
def setText(self, text):
self._text = text
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
l = QTextMovieLabel('Loading...', 'loading.gif')
l.show()
app.exec_()
这篇关于在 QLabel 中显示 .gif 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文