凌的博客

您现在的位置是:首页  > 学无止境 > python

python

python 绘制动态图形

2019-10-05python0

1.png

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QDesktopWidget
from PyQt5.QtCore import Qt, QPoint,QTimer
from PyQt5.QtGui import QPainter, QColor,QBrush
from random import random

class Rect:
    def __init__(self):
        self.start = QPoint()
        self.end = QPoint()

    def setStart(self, s):
        self.start = s

    def setEnd(self, e):
        self.end = e

    def startPoint(self):
        return self.start

    def endPoint(self):
        return self.end

    def paint(self, painter):
        painter.drawRect(self.startPoint().x(), self.startPoint().y(), self.endPoint().x() - self.startPoint().x(),
                         self.endPoint().y() - self.startPoint().y())


class Line(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setMouseTracking(True)  # 保证得到鼠标信息


        self.timer = QTimer()
        self.timer.setInterval(100)
        self.timer.timeout.connect(self.update)
        self.timer.start()

        self.setGeometry(300, 300, 500, 500)
        self.setWindowTitle("绘画")
        self.center()


    def paintEvent(self, ev):
        x1 = int(255 * random())
        y1 = int(255 * random())
        x2 = int(255 * random())
        y2 = int(255 * random())
        print(x1, y1, x2, y2)
        painter = QPainter(self)
        painter.save()
        painter.setPen(QColor(166,66,250))
        #painter.begin(self)
        #self.painter.drawLine(100, 100, 200, 200)
        painter.drawLine(x1, y1, x2, y2)
        # 绘制函数
        #painter.end()
        painter.restore()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    line = Line()
    line.show()
    sys.exit(app.exec_())

#################

绘制时钟

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from math import *
import sys


class clockForm(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Clock")
        self.timer = QTimer()
        # 设置窗口计时器
        self.timer.timeout.connect(self.update)
        self.timer.start(1000)

    def paintEvent(self, event):
        # 时钟指针坐标点
        hourPoint = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -30)]
        minPoint = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -65)]
        secPoint = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -80)]
        # 时钟指针颜色
        hourColor = QColor(200, 100, 0, 200)
        minColor = QColor(0, 127, 127, 150)
        secColor = QColor(0, 160, 230, 150)

        side = min(self.width(), self.height())

        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.translate(self.width() / 2, self.height() / 2)  # painter坐标系原点移至widget中央
        painter.scale(side / 200, side / 200)  # 缩放painterwidget坐标系,使绘制的时钟位于widge中央,即钟表支持缩放

        # 绘制小时和分钟刻度线
        painter.save()
        for i in range(0, 60):
            if (i % 5) != 0:
                painter.setPen(minColor)
                painter.drawLine(92, 0, 96, 0)  # 绘制分钟刻度线
            else:
                painter.setPen(hourColor)
                painter.drawLine(88, 0, 96, 0)  # 绘制小时刻度线
            painter.rotate(360 / 60)
        painter.restore()

        # 绘制小时数字
        painter.save()
        font = painter.font()
        font.setBold(True)
        painter.setFont(font)
        pointSize = font.pointSize()
        painter.setPen(hourColor)
        nhour = 0
        radius = 100
        for i in range(0, 12):
            nhour = i + 3  # 按QT-Qpainter的坐标系换算,3小时的刻度线对应坐标轴0度
            if nhour > 12:
                nhour = nhour - 12

            x = radius * 0.8 * cos(i * 30 * pi / 180.0) - pointSize
            y = radius * 0.8 * sin(i * 30 * pi / 180.0) - pointSize / 2.0
            width = pointSize * 2
            height = pointSize
            painter.drawText(QRectF(x, y, width, height), Qt.AlignCenter, str(nhour))
        painter.restore()

        time = QTime.currentTime()

        # 绘制小时指针
        painter.save()
        painter.setPen(Qt.NoPen)  # 无轮廓线
        painter.setBrush(hourColor)  # 填充色
        painter.rotate(30 * (time.hour() + time.minute() / 60))  # 每圈360° = 12h 即:旋转角度 = 小时数 * 30°
        print(time.hour())
        painter.drawConvexPolygon(QPolygonF(hourPoint))
        painter.restore()

        # 绘制分钟指针
        painter.save()
        painter.setPen(Qt.NoPen)  # 无轮廓线
        painter.setBrush(minColor)  # 填充色
        painter.rotate(6 * (time.minute() + time.second() / 60))  # 每圈360° = 60m 即:旋转角度 = 分钟数 * 6°
        painter.drawConvexPolygon(QPolygonF(minPoint))
        painter.restore()

        # 绘制秒钟指针
        painter.save()
        painter.setPen(Qt.NoPen)  # 无轮廓线
        painter.setBrush(secColor)  # 填充色
        painter.rotate(6 * time.second())
        painter.drawConvexPolygon(QPolygonF(secPoint))  # 每圈360° = 60s 即:旋转角度 = 秒数 * 6°
        painter.restore()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = clockForm()
    form.show()
    app.exec_()


如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论