• 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_())
  • 相关阅读:
    Apache Tomcat 6.0 Tomcat6 服务因 1 (0x1) 服务特定错误而停止
    PaodingAnalysis 提示 "dic home should not be a file, but a directory"
    mappingDirectoryLocations
    多级反向代理下,Java获取请求客户端的真实IP地址多中方法整合
    java.util.ResourceBundle
    JSP验证码
    Error: [ng:areq] Argument 'LoginCtrl' is not a function, got undefined
    《横向领导力》笔记
    Java执行定时任务
    2017第43周三
  • 原文地址:https://www.cnblogs.com/chusiyong/p/12926131.html
Copyright © 2020-2023  润新知