zoukankan      html  css  js  c++  java
  • PyQt5绘图

    QPainter

    功能:QPainter实现在QWidget上画图功能

    说明:绘图必须在paintEvent中完成,且要在bengin和end之间作图

    接口:

    方法 描述
    begin 开始画图
    end 结束画图
    drawLine(int x1, int y1, int x2, int y2) 绘制直线从(x1,y1)到(x2,y2)
    drawRect(int x, int y, int w, int h) 绘制矩形起始坐标(x,y),宽度w,高度h
    drawText 绘制字符串
    FillRect 使用颜色填充矩形
    setBrush 设置画刷
    setPen 设置画笔

    QPen

    功能:画笔,主要是设置线条的格式和颜色

    QBrush

    功能:画刷,主要是设置填充的格式和颜色

    例子

    import random
    import string
    import sys
    
    from PyQt5.QtCore import Qt, QRect
    from PyQt5.QtGui import QPainter, QFont, QColor, QPen, QBrush
    from PyQt5.QtWidgets import QApplication, QWidget
    
    class MyWidget(QWidget):
        def __init__(self):
            super(MyWidget, self).__init__()
    
        # def paintEvent(self, QPaintEvent):
        #     painter = QPainter(self)
        #     painter.begin(self)
        #     painter.setFont(QFont('Courier New', 20))
        #     painter.setPen(QColor(168, 34, 3))
        #     painter.drawText(QPaintEvent.rect(), Qt.AlignCenter, 'hello world')
        #     painter.end()
    
        # def paintEvent(self, QPaintEvent):
        #     painter = QPainter(self)
        #     painter.begin(self)
        #     painter.setFont(QFont('Courier New', 20))
        #     painter.setPen(QColor(168, 34, 3))
        #
        #     for i in range(10000):
        #             painter.drawPoint(random.randint(0, 300), random.randint(0, 300))
        #
        #     painter.end()
    
        # def paintEvent(self, QPaintEvent):
        #     painter = QPainter(self)
        #     painter.begin(self)
        #
        #     pen = QPen(Qt.red, 2, Qt.SolidLine)
        #     painter.setPen(pen)
        #     painter.drawLine(20, 20, 400, 20)
        #
        #     pen.setStyle(Qt.DashDotLine)
        #     painter.setPen(pen)
        #     painter.drawLine(20, 40, 400, 40)
        #
        #     painter.end()
    
        # def paintEvent(self, QPaintEvent):
        #     painter = QPainter(self)
        #     painter.begin(self)
        # 
        #     brush = QBrush(Qt.SolidPattern)
        #     brush.setColor(Qt.green)
        #     painter.setBrush(brush)
        #     painter.drawRect(20, 20, 200, 100)
        # 
        #     brush = QBrush(Qt.HorPattern)
        #     brush.setColor(Qt.red)
        #     painter.setBrush(brush)
        #     painter.drawRect(20, 140, 200, 100)
        # 
        #     painter.end()
    
        # 实现随机验证码
        def paintEvent(self, QPaintEvent):
            painter = QPainter(self)
            painter.begin(self)
            painter.setFont(QFont('Courier New', 50))
            randomString = "".join([random.choice(string.digits + string.ascii_letters) for i in range(10)])
            for i in range(len(randomString)):
                painter.setPen(QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
                painter.drawText(50 + i * 40, 150, randomString[i])
    
            for i in range(25000):
                painter.setPen(QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
                painter.drawPoint(random.randint(0, 500), random.randint(0, 300))
    
            painter.end()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = MyWidget()
        w.resize(500, 300)
        w.move(300, 300)
        w.setWindowTitle('Simple')
        w.show()
        sys.exit(app.exec_())
  • 相关阅读:
    非专业码农 JAVA学习笔记 3 抽象、封装和类(1)
    非计算机专业的码农C#学习笔记 三、变量 表达式 字符串
    非专业码农 JAVA学习笔记 2 java语言基础
    非计算机专业的码农C#学习笔记 五、数组和集合
    PSP个人软件开发工具
    端口映射
    $.proxy()
    input type=button设置高度不管用
    移动端日期控件
    mac终端下svn常用命令
  • 原文地址:https://www.cnblogs.com/chusiyong/p/12926131.html
Copyright © 2011-2022 走看看