PyQT4 图形用户界面更新
Posted
技术标签:
【中文标题】PyQT4 图形用户界面更新【英文标题】:PyQT4 GUI Update 【发布时间】:2015-06-21 13:09:08 【问题描述】:我有一个 PyQT GUI 代码,它从旋转框中获取用户输入(角度),并以指定角度更新 GUI 上的指南针对象。我想使用 for 循环(0 到 360)从代码内部而不是 spinbox 更新指南针对象,中间有一些延迟,以进行类似时钟的运动。请提出一些实现这一目标的方法。
当前代码:
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_())
【问题讨论】:
您的最终目标是“只是”动画,还是来自其他地方的实际角度,可能是外部传感器/输入设备?当前的两个答案将对不同的情况有用。 【参考方案1】:QPropertyAnimation
类正是为此目的而设计的,并且非常易于使用。有一系列内置的easing-curves 可以帮助使动画看起来更自然。
这是基于您的示例脚本的演示。 (请注意,您需要在 spinbox 中按 enter 来更改值):
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_())
【讨论】:
【参考方案2】:使用来自Python PySide and Progress Bar Threading 的示例我得到了
import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Worker(QThread):
updateAngle = pyqtSignal(float)
def __init__(self):
QThread.__init__(self)
def run(self):
for i in range(360):
self.updateAngle.emit(i)
time.sleep(0.2)
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._worker = Worker()
self._worker.updateAngle.connect(self.setAngle)
self._worker.start()
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)
spinBox.valueChanged.connect(compass.setAngle)
layout = QVBoxLayout()
layout.addWidget(compass)
layout.addWidget(spinBox)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
简而言之,如果你使用 Qt,你应该使用 QThread
而不是原生 python 线程。
【讨论】:
此类解决方案的问题在于将值从 20° 更改为 320° 所需的时间。在您的示例脚本中,这将花费 300 * 0.2 = 60 秒 - 这不是非常用户友好! @ekhumoro 你改变单个常数有多难? 嗯,这显然有点很难。您使用什么常数既适用于小变化(例如 20° 到 30°)又适用于大变化(例如 20° 到 320°)?显然,需要进行一些计算才能获得适用于所有类型更改(向前和向后)的间隔。【参考方案3】:如何使用QTimer? 将它的timeout
信号连接到一些增加计数器并告诉指南针小部件更新的方法。
【讨论】:
以上是关于PyQT4 图形用户界面更新的主要内容,如果未能解决你的问题,请参考以下文章
Python+PyQt4+Eric6合并Excel的图形化界面设计