博客
关于我
pyqt5教程11:绘制外观
阅读量:797 次
发布时间:2023-03-05

本文共 6654 字,大约阅读时间需要 22 分钟。

一、PyQt5绘画系统概述

PyQt5提供了强大的绘画功能,能够渲染矢量图形、图像以及基于轮廓字体的文本。无论是对现有小部件进行修改,还是创建自定义小部件,应用程序都需要绘图支持。PyQt5提供了丰富的绘图API,能够满足各种绘图需求。

二、QPainter绘画部件

QPainter是PyQt5中用于绘制的核心工具。它能够在小部件和其他绘画设备上执行低级绘图操作。无论是绘制简单的线条,还是复杂的图形,QPainter都能胜任。它通常在窗口的绘图事件paintEvent()中使用。

QPainter的使用流程如下:

  • 创建QPainter对象。
  • paintEvent()方法中调用begin()方法启动绘图。
  • 执行具体的绘图逻辑。
  • 调用end()方法完成绘图。
  • 调用repaint()update()方法触发下一次绘图。
  • 三、绘图事件方法

    绘图工作始终在paintEvent()方法中执行。QPainter的所有绘图方法需要在begin()end()之间进行。具体来说:

    def paintEvent(self, event):    qp = QPainter()    qp.begin(self)    self.drawText(event, qp)    qp.end()

    四、PyQt5绘制文本

    要在窗口中绘制文本,可以参考以下代码示例:

    import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainter, QColor, QFontfrom PyQt5.QtCore import Qtclass Example(QWidget):    def __init__(self):        super().__init__()        self.text = "Лев Николаевич Толстой\nАнна Каренина"        self.setGeometry(300, 300, 350, 300)        self.setWindowTitle('Drawing text')    def paintEvent(self, event):        qp = QPainter()        qp.begin(self)        self.drawText(event, qp)        qp.end()    def drawText(self, event, qp):        qp.setPen(QColor(168, 34, 3))        qp.setFont(QFont('Decorative', 10))        qp.drawText(event.rect(), Qt.AlignCenter, self.text)def main():    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

    五、PyQt5绘制点

    要绘制多个随机点,可以参考以下代码示例:

    import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainterfrom PyQt5.QtCore import Qtclass Example(QWidget):    def __init__(self):        super().__init__()        self.setGeometry(300, 300, 300, 190)        self.setWindowTitle('Points')    def paintEvent(self, e):        qp = QPainter()        qp.begin(self)        self.drawPoints(qp)        qp.end()    def drawPoints(self, qp):        qp.setPen(Qt.red)        size = self.size()        if size.height() <= 1:            return        for i in range(1000):            x = random.randint(1, size.width() - 1)            y = random.randint(1, size.height() - 1)            qp.drawPoint(x, y)def main():    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

    六、PyQt5颜色表达

    PyQt5支持多种颜色表示方式,包括RGB和RGBA值。以下代码示例展示了如何使用不同的颜色来绘制矩形:

    import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainter, QColor, QBrushimport sysclass Example(QWidget):    def __init__(self):        super().__init__()        self.setGeometry(300, 300, 350, 100)        self.setWindowTitle('Colours')    def paintEvent(self, e):        qp = QPainter()        qp.begin(self)        self.drawRectangles(qp)        qp.end()    def drawRectangles(self, qp):        col = QColor(0, 0, 0)        col.setNamedColor('#d4d4d4')        qp.setPen(col)        qp.setBrush(QColor(200, 0, 0))        qp.drawRect(10, 15, 90, 60)        qp.setBrush(QColor(255, 80, 0, 160))        qp.drawRect(130, 15, 90, 60)        qp.setBrush(QColor(25, 0, 90, 200))        qp.drawRect(250, 15, 90, 60)def main():    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

    七、PyQt5画笔QPen

    QPen是PyQt5中用于绘制图形边缘的工具。以下代码示例展示了如何使用不同笔样式绘制线条:

    import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainter, QPenfrom PyQt5.QtCore import Qtclass Example(QWidget):    def __init__(self):        super().__init__()        self.setGeometry(300, 300, 280, 270)        self.setWindowTitle('Pen styles')    def paintEvent(self, e):        qp = QPainter()        qp.begin(self)        self.drawLines(qp)        qp.end()    def drawLines(self, qp):        pen = QPen(Qt.black, 2, Qt.SolidLine)        qp.setPen(pen)        qp.drawLine(20, 40, 250, 40)        pen.setStyle(Qt.DashLine)        qp.setPen(pen)        qp.drawLine(20, 80, 250, 80)        pen.setStyle(Qt.DashDotLine)        qp.setPen(pen)        qp.drawLine(20, 120, 250, 120)        pen.setStyle(Qt.DotLine)        qp.setPen(pen)        qp.drawLine(20, 160, 250, 160)        pen.setStyle(Qt.DashDotDotLine)        qp.setPen(pen)        qp.drawLine(20, 200, 250, 200)        pen.setStyle(Qt.CustomDashLine)        pen.setDashPattern([1, 4, 5, 4])        qp.setPen(pen)        qp.drawLine(20, 240, 250, 240)def main():    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

    八、PyQt5画刷QBrush

    QBrush是PyQt5中用于绘制图形内部的工具。以下代码示例展示了如何使用不同画刷风格绘制矩形:

    import sysfrom PyQt5.QtWidgets import QWidget, QApplicationfrom PyQt5.QtGui import QPainter, QBrushfrom PyQt5.QtCore import Qtclass Example(QWidget):    def __init__(self):        super().__init__()        self.setGeometry(300, 300, 355, 280)        self.setWindowTitle('Brushes')    def paintEvent(self, e):        qp = QPainter()        qp.begin(self)        self.drawBrushes(qp)        qp.end()    def drawBrushes(self, qp):        brush = QBrush(Qt.SolidPattern)        qp.setBrush(brush)        qp.drawRect(10, 15, 90, 60)        brush.setStyle(Qt.Dense1Pattern)        qp.setBrush(brush)        qp.drawRect(130, 15, 90, 60)        brush.setStyle(Qt.Dense2Pattern)        qp.setBrush(brush)        qp.drawRect(250, 15, 90, 60)        brush.setStyle(Qt.DiagCrossPattern)        qp.setBrush(brush)        qp.drawRect(10, 105, 90, 60)        brush.setStyle(Qt.Dense5Pattern)        qp.setBrush(brush)        qp.drawRect(130, 105, 90, 60)        brush.setStyle(Qt.Dense6Pattern)        qp.setBrush(brush)        qp.drawRect(250, 105, 90, 60)        brush.setStyle(Qt.HorPattern)        qp.setBrush(brush)        qp.drawRect(10, 195, 90, 60)        brush.setStyle(Qt.VerPattern)        qp.setBrush(brush)        qp.drawRect(130, 195, 90, 60)        brush.setStyle(Qt.BDiagPattern)        qp.setBrush(brush)        qp.drawRect(250, 195, 90, 60)def main():    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

    九、PyQt5贝塞尔曲线

    PyQt5支持绘制贝塞尔曲线。以下代码示例展示了如何使用QPainterPath绘制贝塞尔曲线:

    import sysfrom PyQt5.QtGui import QPainter, QPainterPathfrom PyQt5.QtWidgets import QWidget, QApplicationclass Example(QWidget):    def __init__(self):        super().__init__()        self.setGeometry(300, 300, 380, 250)        self.setWindowTitle('Bézier curve')    def paintEvent(self, e):        qp = QPainter()        qp.begin(self)        qp.setRenderHint(QPainter.Antialiasing)        self.drawBezierCurve(qp)        qp.end()    def drawBezierCurve(self, qp):        path = QPainterPath()        path.moveTo(30, 30)        path.cubicTo(30, 30, 200, 350, 350, 30)        qp.drawPath(path)def main():    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

    转载地址:http://ucafk.baihongyu.com/

    你可能感兴趣的文章
    PyCharm 搜索与导航栏详解
    查看>>
    PyCharm 搭建 Selenium + Python 的自动化测试环境
    查看>>
    PyCharm 数据库操作详解
    查看>>
    PyCharm 版本与管理详解
    查看>>
    PyCharm 界面排版详解
    查看>>
    pycharm 配置qt
    查看>>
    Pycharm+Django搭建第一个Python Web程序
    查看>>
    pycharm+pyqt5配置
    查看>>
    PyCharm不突出显示错误
    查看>>
    pycharm中windows找不到chrome解决办法
    查看>>
    Pycharm中基于Ollama和Proxy AI使用本地算力进行AI辅助编程
    查看>>
    Pycharm中配置项目的打开方式(This Window,New Window)
    查看>>
    PyCharm为什么这么牛?
    查看>>
    Pycharm出现 Cannot find declaration to go to 解决方法(全)
    查看>>
    pycharm出现Can‘t get remote credentials for deployment server 的解决方法
    查看>>
    pycharm可视化数据库
    查看>>
    pytorch 中的 #@save的意思
    查看>>
    Pycharm如何取消自动换行
    查看>>
    PyCharm安装及使用说明
    查看>>
    pycharm导入自己写的模块时,模块下方出现红色波浪线的解决方案
    查看>>