gpt4 book ai didi

python - PyQT4 图形用户界面更新

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:22 25 4
gpt4 key购买 nike

我有一个 PyQT GUI 代码,它从旋转框获取用户输入(角度)并以指定的角度更新 GUI 上的罗盘对象。我想使用 for 循环(0 到 360)从代码内部更新罗盘对象而不是旋转框,中间有一些延迟,以进行类似时钟的运动。请提出一些实现该目标的方法。

当前代码:

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


#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
class CompassWidget(QWidget):

angleChanged = pyqtSignal(float)

def __init__(self, parent = None):

QWidget.__init__(self, parent)

self._angle = 0.0
self._margins = 10
self._pointText = {0: "N", 45: "NE", 90: "E", 135: "SE", 180: "S",
225: "SW", 270: "W", 315: "NW"}

def paintEvent(self, event):

painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)

painter.fillRect(event.rect(), self.palette().brush(QPalette.Window))
self.drawMarkings(painter)
self.drawNeedle(painter)

painter.end()

def drawMarkings(self, painter):

painter.save()
painter.translate(self.width()/2, self.height()/2)
scale = min((self.width() - self._margins)/120.0,
(self.height() - self._margins)/120.0)
painter.scale(scale, scale)

font = QFont(self.font())
font.setPixelSize(10)
metrics = QFontMetricsF(font)

painter.setFont(font)
painter.setPen(self.palette().color(QPalette.Shadow))

i = 0
while i < 360:
if i % 45 == 0:
painter.drawLine(0, -40, 0, -50)
painter.drawText(-metrics.width(self._pointText[i])/2.0, -52,self._pointText[i])
else:
painter.drawLine(0, -45, 0, -50)
painter.rotate(1)
i += 1

painter.restore()

def drawNeedle(self, painter):

painter.save()
painter.translate(self.width()/2, self.height()/2)
painter.rotate(self._angle)
scale = min((self.width() - self._margins)/120.0,
(self.height() - self._margins)/120.0)
painter.scale(scale, scale)

painter.setPen(QPen(Qt.NoPen))
painter.setBrush(self.palette().brush(QPalette.Shadow))

painter.drawPolygon(
QPolygon([QPoint(-10, 0), QPoint(0, -45), QPoint(10, 0),
QPoint(0, 45), QPoint(-10, 0)])
)

painter.setBrush(self.palette().brush(QPalette.Highlight))

painter.drawPolygon(
QPolygon([QPoint(-5, -25), QPoint(0, -45), QPoint(5, -25),
QPoint(0, -30), QPoint(-5, -25)])
)

painter.restore()

def sizeHint(self):

return QSize(600, 600)

def angle(self):
return self._angle

@pyqtSlot(float)
def setAngle(self, angle):

if angle != self._angle:
self._angle = angle
self.angleChanged.emit(angle)
self.update()

angle = pyqtProperty(float, angle, setAngle)

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

if __name__ == "__main__":

app = QApplication(sys.argv)

window = QWidget()
compass = CompassWidget()
spinBox = QSpinBox()
spinBox.setRange(0, 359)

#compass.angleChanged.connect(compass.setAngle)
spinBox.valueChanged.connect(compass.setAngle)

layout = QVBoxLayout()
layout.addWidget(compass)
layout.addWidget(spinBox)
window.setLayout(layout)

window.show()

sys.exit(app.exec_())

最佳答案

QPropertyAnimation类正是为此目的而设计的,并且非常易于使用。有一系列内置 easing-curves这有助于使动画看起来更自然。

这是一个基于您的示例脚本的演示。 (注意,您需要在旋转框中按回车键来更改值):

class CompassWidget(QWidget):

angleChanged = pyqtSignal(float)

def __init__(self, parent = None):

QWidget.__init__(self, parent)

self._angle = 0.0
self._margins = 10
self._pointText = {0: "N", 45: "NE", 90: "E", 135: "SE", 180: "S",
225: "SW", 270: "W", 315: "NW"}

self.animation = QPropertyAnimation(self, 'angle')
self.animation.setEasingCurve(QEasingCurve.InOutExpo)
self.animation.setDuration(1500)

def paintEvent(self, event):

painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)

painter.fillRect(event.rect(), self.palette().brush(QPalette.Window))
self.drawMarkings(painter)
self.drawNeedle(painter)

painter.end()

def drawMarkings(self, painter):

painter.save()
painter.translate(self.width()/2, self.height()/2)
scale = min((self.width() - self._margins)/120.0,
(self.height() - self._margins)/120.0)
painter.scale(scale, scale)

font = QFont(self.font())
font.setPixelSize(10)
metrics = QFontMetricsF(font)

painter.setFont(font)
painter.setPen(self.palette().color(QPalette.Shadow))

i = 0
while i < 360:
if i % 45 == 0:
painter.drawLine(0, -40, 0, -50)
painter.drawText(-metrics.width(self._pointText[i])/2.0, -52,self._pointText[i])
else:
painter.drawLine(0, -45, 0, -50)
painter.rotate(1)
i += 1

painter.restore()

def drawNeedle(self, painter):

painter.save()
painter.translate(self.width()/2, self.height()/2)
painter.rotate(self._angle)
scale = min((self.width() - self._margins)/120.0,
(self.height() - self._margins)/120.0)
painter.scale(scale, scale)

painter.setPen(QPen(Qt.NoPen))
painter.setBrush(self.palette().brush(QPalette.Shadow))

painter.drawPolygon(
QPolygon([QPoint(-10, 0), QPoint(0, -45), QPoint(10, 0),
QPoint(0, 45), QPoint(-10, 0)])
)

painter.setBrush(self.palette().brush(QPalette.Highlight))

painter.drawPolygon(
QPolygon([QPoint(-5, -25), QPoint(0, -45), QPoint(5, -25),
QPoint(0, -30), QPoint(-5, -25)])
)

painter.restore()

def sizeHint(self):

return QSize(600, 600)

def angle(self):
return self._angle

def setAngle(self, angle):
if angle != self._angle:
self._angle = angle
self.update()

angle = pyqtProperty(float, angle, setAngle)

def animate(self, angle):
self.animation.setStartValue(self._angle)
self.animation.setEndValue(angle)
self.animation.start()

if __name__ == "__main__":

app = QApplication(sys.argv)

window = QWidget()
compass = CompassWidget()
spinBox = QSpinBox()
spinBox.setRange(0, 359)

spinBox.editingFinished.connect(
lambda: compass.animate(spinBox.value()))

layout = QVBoxLayout()
layout.addWidget(compass)
layout.addWidget(spinBox)
window.setLayout(layout)

window.show()

sys.exit(app.exec_())

关于python - PyQT4 图形用户界面更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964856/

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