import random
from PySide6.QtWidgets import QApplication, QWidget
from PySide6.QtGui import QPainter, QPen
from PySide6.QtCore import Qt, QTimer
class MyWidget(QWidget):
def __init__(self):
# 坐标参数
super().__init__()
self.x_points = [0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380,
400]
self.y_points = []
def paintEvent(self, event):
painter = QPainter(self)
pen = QPen(Qt.red)
pen.setWidth(1)
painter.setPen(pen)
# 绘制折线
if len(self.y_points) > 1:
i = 1
while i < len(self.y_points):
painter.drawLine(self.x_points[i - 1], self.y_points[i - 1], self.x_points[i], self.y_points[i])
i += 1
def pretreat(): # 数据的预处理
n = random.randint(10, 390)
widget.y_points.append(n)
if len(widget.y_points) > 21:
del (widget.y_points[0])
print(widget.y_points)
if __name__ == "__main__":
app = QApplication([])
timer = QTimer()
timer.start(500)
widget = MyWidget()
widget.resize(400, 400)
timer.timeout.connect(pretreat)
timer.timeout.connect(widget.update)
widget.show()
app.exec()
pyside6画直线的基本demo
最新推荐文章于 2024-10-13 14:16:02 发布
本文介绍了如何使用Python的PySide6库创建一个简单的GUI应用,实现在窗口中动态绘制随机生成的折线图,通过定时器每500毫秒更新数据点并重新绘制。
摘要由CSDN通过智能技术生成