gpt4 book ai didi

python - 在带有 QT4 的 Python 中使用像素图或图像序列制作动画

转载 作者:太空宇宙 更新时间:2023-11-04 06:33:54 27 4
gpt4 key购买 nike

我有一个小的 Python 脚本,可以创建一个透明窗口来在屏幕上显示图形,我想为该图形设置动画,但我完全不确定如何或从哪里开始。这是我至少拥有的:

import sys
from PyQt4 import QtGui, Qt, QtCore

class Transparent(QtGui.QWidget):

def __init__(self):
QtGui.QWidget.__init__(self)
self.setAttribute(Qt.Qt.WA_NoSystemBackground)
self.setAutoFillBackground(True)

pixmap = QtGui.QPixmap("test1.gif")
pixmap2 = QtGui.QPixmap("test2.gif")
width = pixmap.width()
height = pixmap.height()

self.setWindowTitle("Status")
self.resize(width, height)

self.label = QtGui.QLabel(self)


def animateEvent():
imgnumber = 0
try:
if imgnumber == 1:
self.label.setPixmap(QtGui.QPixmap("test1.gif"))
self.setMask(pixmap.mask())
imgnumber = 0
else:
self.label.setPixmap(QtGui.QPixmap("test2.gif"))
self.setMask(pixmap2.mask())
imgnumber = 1
finally:
QtCore.QTimer.singleShot(1000, animateEvent)

animateEvent()


def paintEvent(self,event):
self.setAttribute(Qt.Qt.WA_NoSystemBackground)

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
x = Transparent()
x.show()
app.exec_()

感觉它的成分正确,但像素图没有更新。

我尝试了 QMovie,但是窗口中本应透明的区域被黑色填充了。

最佳答案

www.daniweb.com 查看此代码并查看是否可以根据需要修改它:

import sys 
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MoviePlayer(QWidget):
def __init__(self, gif, parent=None):
super(MoviePlayer, self).__init__(parent)

self.setGeometry(200, 200, 400, 400)
self.setWindowTitle("QMovie to show animated gif")

self.movie_screen = QLabel()
self.movie_screen.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.movie_screen.setAlignment(Qt.AlignCenter)

btn_start = QPushButton("Start Animation")
btn_start.clicked.connect(self.start)

btn_stop = QPushButton("Stop Animation")
btn_stop.clicked.connect(self.stop)

main_layout = QVBoxLayout()
main_layout.addWidget(self.movie_screen)
main_layout.addWidget(btn_start)
main_layout.addWidget(btn_stop)
self.setLayout(main_layout)

self.movie = QMovie(gif, QByteArray(), self)
self.movie.setCacheMode(QMovie.CacheAll)
self.movie.setSpeed(100)
self.movie_screen.setMovie(self.movie)


def start(self):
"""
Start animation
"""
self.movie.start()

def stop(self):
"""
Stop the animation
"""
self.movie.stop()


app = QApplication(sys.argv)
player = MoviePlayer("/path/to/image.gif")
player.show()
sys.exit(app.exec_())

关于python - 在带有 QT4 的 Python 中使用像素图或图像序列制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783107/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com