gpt4 book ai didi

python - 如何基于 QTimer 更新 QPainter 像素图

转载 作者:行者123 更新时间:2023-11-30 21:54:02 25 4
gpt4 key购买 nike

我想根据 QTimer 的每个刻度连续旋转 QPainter 像素图 - 在本例中是时钟臂。我可以旋转时钟臂,但是我没有使旋转动态化的技能。这是我想要制作的时钟,下面是我的示例代码。如果您能在路上帮助我,请告诉我,谢谢!

enter image description here

import sys
import random

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt

class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)

self.label = QtWidgets.QLabel()
self.setCentralWidget(self.label)

self.Clock_pixmap = QtGui.QPixmap("clock.png")
self.Arm_pixmap = QtGui.QPixmap("clockarm.png")
self.painter = QtGui.QPainter(self.Clock_pixmap)
self.painter.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform)
self.painter.drawPixmap(QtCore.QPoint(), self.Arm_pixmap)
self.painter.end()
self.label.setPixmap(self.Clock_pixmap.scaled(self.label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.label.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.label.setMinimumSize(150, 150)

self.w1 = self.Arm_pixmap.width()/2
self.h1 = self.Arm_pixmap.height()/2

self.rotationData = random.sample(range(100), 100)

timer = QtCore.QTimer(self, timeout=self.rotateArm, interval=100)
timer.start()

self.n=0

def rotateArm(self):
self.n+=1
self.painter.translate(self.w1,self.h1)
self.painter.rotate(self.rotationData[self.n])
self.painter.translate(-self.w1,-self.h1)
self.update()

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

最佳答案

您正在 QPainter 上绘画,您指示它已完成绘画,因为您使用了 end() 方法。所以没有必要为QPainter创建一个类属性,而只需为一个局部变量。综合以上情况,解决办法是:

import sys
import random

from PyQt5 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self._angle = 0

self.label = QtWidgets.QLabel()
self.setCentralWidget(self.label)

self.clock_pixmap = QtGui.QPixmap("clock.png")
self.arm_pixmap = QtGui.QPixmap("clockarm.png")

rotation_data = random.sample(range(100), 100)
self.data_iter = iter(rotation_data)

timer = QtCore.QTimer(self, timeout=self.rotate_arm, interval=100)
timer.start()

def rotate_arm(self):
try:
angle = next(self.data_iter)
except StopIteration:
pass
else:
self.draw(angle)

def draw(self, angle):
pixmap = self.clock_pixmap.copy()
painter = QtGui.QPainter(pixmap)
painter.setRenderHints(
QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform
)
painter.translate(pixmap.rect().center())
painter.rotate(angle)
painter.translate(-pixmap.rect().center())
painter.drawPixmap(QtCore.QPoint(), self.arm_pixmap)
painter.end()
self.label.setPixmap(pixmap)


if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

关于python - 如何基于 QTimer 更新 QPainter 像素图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59431968/

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