九、PyQt5实现Python界面设计_绘图工具QPainter

目录

一、介绍

基本属性成员

基本绘制函数

二、实例演示

(1)绘制文本

(2)使用像素点绘制正弦曲线

(3)绘制不同类型的直线

(4)绘制不同的多边形

(5)用笔刷填充图形区域


一、介绍

QPainter类是Qt中可以在小部件或者其他绘图设备上执行的低级绘制,可以通过该类进行一些简单的直线绘图、文字书写、或者较为复杂点的多表现绘图包括一些简单的函数。

基本属性成员

Qpen 画笔工具,用来设置绘图时的颜色、宽度、线风格等参数,作为画笔使用
QBrush 笔刷工具,用于作为几何图片的调色板,设置其填充的颜色与填充风格
QFont 字体工具,设置文字书写时的字体样式
QPixmap 图片绘制工具,适用于小图片,可以加速显示
QImage 图片绘制工具,适合读取大图片文件进行显示
QBitmap 图片绘制工具,主要用于显示单色位图
QPicture 图片绘制工具,用于记录和重播Qpainter的绘图指令

基本绘制函数

drawPoint 绘制点
drawLine 绘制直线
drawRect 绘制矩形
drawArc

绘制圆弧

drawEllipse 绘制椭圆
drawPie 绘制扇形
drawChord 绘制弦
drawPolygon 绘制多边形
drawRoundedRect 绘制圆角矩形
drawPolyline 绘制折线
drawConvexPolygon 绘制多边形

二、实例演示

(1)绘制文本

'''
绘图API:绘制文本
1. 文本
2. 各种图形(直线,点,椭圆,弧,扇形,多边形等)
3. 图像
QPainter
painter = QPainter()
painter.begin()
painter.drawText(...)
painter.end()
必须在paintEvent事件方法中绘制各种元素
'''

import sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtGui import QPainter,QColor,QFont
from PyQt5.QtCore import Qt

class DrawText(QWidget):
    def __init__(self):
        super(DrawText,self).__init__()
        self.setWindowTitle('在窗口上绘制文本')
        self.resize(400,200)
        self.text = "快闪开,我要开始装逼了"
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.begin(self)
        painter.setPen(QColor(150,43,5))
        painter.setFont(QFont('SimSun',25))
        painter.drawText(event.rect(),Qt.AlignCenter,self.text)
    
        painter.end()
        
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = DrawText()
    main.show()
    app.exec_()
    del app

(2)使用像素点绘制正弦曲线

'''
用像素点绘制正弦曲线
-2PI  2PI
drawPoint(x,y)
'''

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

class DrawPoints(QWidget):
    def __init__(self):
        super(DrawPoints,self).__init__()
        self.resize(300,300)
        self.setWindowTitle('在窗口上用像素点绘制2个周期的正弦曲线')

    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        painter.setPen(Qt.blue)
        size = self.size()

        for i in range(1000):
            x = int(100 * (-1 + 2.0 * i/1000) + size.width()/2.0)
            y = int(-50 * math.sin((x - size.width()/2.0) * math.pi/50) + size.height()/2.0)
            painter.drawPoint(x,y)

        painter.end()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = DrawPoints()
    main.show()
    app.exec_()
    del app

(3)绘制不同类型的直线

'''
绘制不同类型的直线
'''

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

class DrawMultiLine(QWidget):
    def __init__(self):
        super(DrawMultiLine,self).__init__()
        self.resize(300,300)
        self.setWindowTitle('设置Pen的样式')

    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)


        pen = QPen(Qt.red,3,Qt.SolidLine)

        painter.setPen(pen)
        painter.drawLine(20,40,250,40)

        pen.setStyle(Qt.DashLine)
        painter.setPen(pen)
        painter.drawLine(20, 80, 250, 80)

        pen.setStyle(Qt.DashDotDotLine)
        painter.setPen(pen)
        painter.drawLine(20, 120, 250, 120)

        pen.setStyle(Qt.DotLine)
        painter.setPen(pen)
        painter.drawLine(20, 160, 250, 160)

        pen.setStyle(Qt.DashDotDotLine)
        painter.setPen(pen)
        painter.drawLine(20, 200, 250, 200)

        pen.setStyle(Qt.CustomDashLine)
        pen.setDashPattern([1,10,5,8])
        painter.setPen(pen)
        painter.drawLine(20, 240, 250, 240)


        size = self.size()



        painter.end()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = DrawMultiLine()
    main.show()
    app.exec_()
    del app

(4)绘制不同的多边形

'''
绘制各种图形
弧
圆形
椭圆
矩形(正方形)
多边形
绘制图像
'''

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

class DrawAll(QWidget):
    def __init__(self):
        super(DrawAll,self).__init__()
        self.resize(300,600)
        self.setWindowTitle('绘制各种图形')
    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)

        qp.setPen(Qt.blue)

        # 绘制弧
        rect = QRect(0,10,100,100)
        # alen: 1个alen等于1/16度   45 * 16
        qp.drawArc(rect,0, 50 * 16)
        # 通过弧绘制圆
        qp.setPen(Qt.red)
        qp.drawArc(120,10,100,100,0, 360 * 16)

        # 绘制带弦的弧
        qp.drawChord(10,120,100,100,12,130*16)

        # 绘制扇形
        qp.drawPie(10,240,100,100,12,130*16)

        # 椭圆
        qp.drawEllipse(120,120,150,100)
        # 绘制5边形
        point1 = QPoint(140,380)
        point2 = QPoint(270,420)
        point3 = QPoint(290,512)
        point4 = QPoint(290,588)
        point5 = QPoint(200,533)

        polygon = QPolygon([point1,point2,point3,point4,point5])
        qp.drawPolygon(polygon)

        # 绘制图像
        image = QImage('./images/book1.png')
        rect = QRect(10, 400, int(image.width()/3), int(image.height()/3))
        #image.save('./images/book1.png')
        qp.drawImage(rect,image)

        qp.end()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = DrawAll()
    main.show()
    app.exec_()
    del app

(5)用笔刷填充图形区域

'''
用画刷填充图形区域
'''

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

class FillRect(QWidget):
    def __init__(self):
        super(FillRect,self).__init__()
        self.resize(600,600)
        self.setWindowTitle('用画刷填充区域')

    def paintEvent(self,e):
        qp = QPainter()
        qp.begin(self)
        brush = QBrush(Qt.SolidPattern)
        qp.setBrush(brush)
        qp.drawRect(10,15,90,60)

        brush = QBrush(Qt.Dense1Pattern)
        qp.setBrush(brush)
        qp.drawRect(130,15,90,60)

        brush = QBrush(Qt.Dense2Pattern)
        qp.setBrush(brush)
        qp.drawRect(250,15,90,60)

        brush = QBrush(Qt.Dense3Pattern)
        qp.setBrush(brush)
        qp.drawRect(10, 105, 90, 60)

        brush = QBrush(Qt.HorPattern)
        qp.setBrush(brush)
        qp.drawRect(130, 105, 90, 60)
        qp.end()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = FillRect()
    main.show()
    app.exec_()
    del app

你可能感兴趣的:(python,PyQt5,python,qt,开发语言)