原文: http://zetcode.com/gui/pyqt5/painting/

PyQt5 绘图系统能够呈现向量图形,图像和轮廓基于字体的文本。 当我们想要更改或增强现有的小部件,或者从头开始创建自定义小部件时,应用中需要绘图。 要进行绘制,我们使用 PyQt5 工具包提供的绘制 API。

QPainter

QPainter在小部件和其他绘图设备上执行低级绘图。 它可以绘制从简单的线条到复杂形状的所有内容。

paintEvent方法

绘图是在paintEvent()方法中完成的。 绘图代码位于QPainter对象的begin()end()方法之间。 它在小部件和其他绘图设备上执行低级绘图。

绘制文字

我们首先在窗口的客户区域上绘制一些 Unicode 文本。

drawingtext.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example, we draw text in Russian Cylliric.
  6. Author: Jan Bodnar
  7. Website: zetcode.com
  8. Last edited: August 2017
  9. """
  10. import sys
  11. from PyQt5.QtWidgets import QWidget, QApplication
  12. from PyQt5.QtGui import QPainter, QColor, QFont
  13. from PyQt5.QtCore import Qt
  14. class Example(QWidget):
  15. def __init__(self):
  16. super().__init__()
  17. self.initUI()
  18. def initUI(self):
  19. self.text = "Лев Николаевич Толстой\nАнна Каренина"
  20. self.setGeometry(300, 300, 280, 170)
  21. self.setWindowTitle('Drawing text')
  22. self.show()
  23. def paintEvent(self, event):
  24. qp = QPainter()
  25. qp.begin(self)
  26. self.drawText(event, qp)
  27. qp.end()
  28. def drawText(self, event, qp):
  29. qp.setPen(QColor(168, 34, 3))
  30. qp.setFont(QFont('Decorative', 10))
  31. qp.drawText(event.rect(), Qt.AlignCenter, self.text)
  32. if __name__ == '__main__':
  33. app = QApplication(sys.argv)
  34. ex = Example()
  35. sys.exit(app.exec_())

在我们的示例中,我们以西里尔字母绘制一些文本。 文本在垂直和水平方向上对齐。

  1. def paintEvent(self, event):
  2. ...

绘制是在绘画事件中完成的。

  1. qp = QPainter()
  2. qp.begin(self)
  3. self.drawText(event, qp)
  4. qp.end()

QPainter类负责所有低级绘图。 所有绘图方法都在begin()end()方法之间。 实际绘图将委托给drawText()方法。

  1. qp.setPen(QColor(168, 34, 3))
  2. qp.setFont(QFont('Decorative', 10))

在这里,我们定义了用于绘制文本的笔和字体。

  1. qp.drawText(event.rect(), Qt.AlignCenter, self.text)

drawText()方法在窗口上绘制文本。 绘画事件的rect()方法返回需要更新的矩形。 使用Qt.AlignCenter,我们可以在两个维度上对齐文本。

PyQt5 中的绘图 - 图1

图:绘制文本

绘制点

点是可以绘制的最简单的图形对象。 这是窗口上的一个小地方。

points.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In the example, we draw randomly 1000 red points
  6. on the window.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import QWidget, QApplication
  12. from PyQt5.QtGui import QPainter
  13. from PyQt5.QtCore import Qt
  14. import sys, random
  15. class Example(QWidget):
  16. def __init__(self):
  17. super().__init__()
  18. self.initUI()
  19. def initUI(self):
  20. self.setGeometry(300, 300, 300, 190)
  21. self.setWindowTitle('Points')
  22. self.show()
  23. def paintEvent(self, e):
  24. qp = QPainter()
  25. qp.begin(self)
  26. self.drawPoints(qp)
  27. qp.end()
  28. def drawPoints(self, qp):
  29. qp.setPen(Qt.red)
  30. size = self.size()
  31. for i in range(1000):
  32. x = random.randint(1, size.width()-1)
  33. y = random.randint(1, size.height()-1)
  34. qp.drawPoint(x, y)
  35. if __name__ == '__main__':
  36. app = QApplication(sys.argv)
  37. ex = Example()
  38. sys.exit(app.exec_())

在我们的示例中,我们在窗口的客户区域上随机绘制了 1000 个红点。

  1. qp.setPen(Qt.red)

我们将笔设置为红色。 我们使用预定义的Qt.red颜色常量。

  1. size = self.size()

每次我们调整窗口大小时,都会生成一个绘制事件。 我们使用size()方法获得窗口的当前大小。 我们使用窗口的大小将点分布在整个窗口的客户区域中。

  1. qp.drawPoint(x, y)

我们用drawPoint()方法画点。

PyQt5 中的绘图 - 图2

图:点

颜色

颜色是代表红色,绿色和蓝色(RGB)强度值的组合的对象。 有效的 RGB 值在 0 到 255 之间。我们可以通过多种方式定义颜色。 最常见的是 RGB 十进制值或十六进制值。 我们还可以使用代表红色,绿色,蓝色和 Alpha 的 RGBA 值。 在这里,我们添加了一些有关透明度的额外信息。 Alpha 值为 255 表示完全不透明,0 表示完全透明,例如颜色是不可见的。

colours.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. This example draws three rectangles in three
  6. #different colours.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import QWidget, QApplication
  12. from PyQt5.QtGui import QPainter, QColor, QBrush
  13. import sys
  14. class Example(QWidget):
  15. def __init__(self):
  16. super().__init__()
  17. self.initUI()
  18. def initUI(self):
  19. self.setGeometry(300, 300, 350, 100)
  20. self.setWindowTitle('Colours')
  21. self.show()
  22. def paintEvent(self, e):
  23. qp = QPainter()
  24. qp.begin(self)
  25. self.drawRectangles(qp)
  26. qp.end()
  27. def drawRectangles(self, qp):
  28. col = QColor(0, 0, 0)
  29. col.setNamedColor('#d4d4d4')
  30. qp.setPen(col)
  31. qp.setBrush(QColor(200, 0, 0))
  32. qp.drawRect(10, 15, 90, 60)
  33. qp.setBrush(QColor(255, 80, 0, 160))
  34. qp.drawRect(130, 15, 90, 60)
  35. qp.setBrush(QColor(25, 0, 90, 200))
  36. qp.drawRect(250, 15, 90, 60)
  37. if __name__ == '__main__':
  38. app = QApplication(sys.argv)
  39. ex = Example()
  40. sys.exit(app.exec_())

在我们的示例中,我们绘制了三个彩色矩形。

  1. color = QColor(0, 0, 0)
  2. color.setNamedColor('#d4d4d4')

在这里,我们使用十六进制符号定义颜色。

  1. qp.setBrush(QColor(200, 0, 0))
  2. qp.drawRect(10, 15, 90, 60)

在这里,我们定义了一个画笔并绘制了一个矩形。笔刷是用于绘制形状背景的基本图形对象。 drawRect()方法接受四个参数。 前两个是轴上的 x 和 y 值。 第三个和第四个参数是矩形的宽度和高度。 该方法使用当前的笔和画笔绘制矩形。

PyQt5 中的绘图 - 图3

图:颜色

QPen是基本图形对象。 它用于绘制矩形,椭圆形,多边形或其他形状的线,曲线和轮廓。

pens.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example we draw 6 lines using
  6. different pen styles.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import QWidget, QApplication
  12. from PyQt5.QtGui import QPainter, QPen
  13. from PyQt5.QtCore import Qt
  14. import sys
  15. class Example(QWidget):
  16. def __init__(self):
  17. super().__init__()
  18. self.initUI()
  19. def initUI(self):
  20. self.setGeometry(300, 300, 280, 270)
  21. self.setWindowTitle('Pen styles')
  22. self.show()
  23. def paintEvent(self, e):
  24. qp = QPainter()
  25. qp.begin(self)
  26. self.drawLines(qp)
  27. qp.end()
  28. def drawLines(self, qp):
  29. pen = QPen(Qt.black, 2, Qt.SolidLine)
  30. qp.setPen(pen)
  31. qp.drawLine(20, 40, 250, 40)
  32. pen.setStyle(Qt.DashLine)
  33. qp.setPen(pen)
  34. qp.drawLine(20, 80, 250, 80)
  35. pen.setStyle(Qt.DashDotLine)
  36. qp.setPen(pen)
  37. qp.drawLine(20, 120, 250, 120)
  38. pen.setStyle(Qt.DotLine)
  39. qp.setPen(pen)
  40. qp.drawLine(20, 160, 250, 160)
  41. pen.setStyle(Qt.DashDotDotLine)
  42. qp.setPen(pen)
  43. qp.drawLine(20, 200, 250, 200)
  44. pen.setStyle(Qt.CustomDashLine)
  45. pen.setDashPattern([1, 4, 5, 4])
  46. qp.setPen(pen)
  47. qp.drawLine(20, 240, 250, 240)
  48. if __name__ == '__main__':
  49. app = QApplication(sys.argv)
  50. ex = Example()
  51. sys.exit(app.exec_())

在我们的示例中,我们绘制了六条线。 线条以六种不同的笔样式绘制。 有五种预定义的笔样式。 我们还可以创建自定义笔样式。 最后一行是使用自定义笔样式绘制的。

  1. pen = QPen(Qt.black, 2, Qt.SolidLine)

我们创建一个QPen对象。 颜色是黑色。 宽度设置为 2 像素,以便我们可以看到笔样式之间的差异。 Qt.SolidLine是预定义的笔样式之一。

  1. pen.setStyle(Qt.CustomDashLine)
  2. pen.setDashPattern([1, 4, 5, 4])
  3. qp.setPen(pen)

在这里,我们定义了自定义笔样式。 我们设置Qt.CustomDashLine笔样式并调用setDashPattern()方法。 数字列表定义样式。 数字必须是偶数。 奇数定义笔划线,偶数空格。 数字越大,空格或笔划线越大。 我们的模式是 1px 笔划线,4px 间隔,5px 笔划线,4px 间隔等。

PyQt5 中的绘图 - 图4

图:笔的样式

QBrush

QBrush是基本图形对象。 它用于绘制图形形状的背景,例如矩形,椭圆形或多边形。 笔刷可以具有三种不同类型:预定义笔刷,渐变或纹理图案。

brushes.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. This example draws nine rectangles in different
  6. brush styles.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import QWidget, QApplication
  12. from PyQt5.QtGui import QPainter, QBrush
  13. from PyQt5.QtCore import Qt
  14. import sys
  15. class Example(QWidget):
  16. def __init__(self):
  17. super().__init__()
  18. self.initUI()
  19. def initUI(self):
  20. self.setGeometry(300, 300, 355, 280)
  21. self.setWindowTitle('Brushes')
  22. self.show()
  23. def paintEvent(self, e):
  24. qp = QPainter()
  25. qp.begin(self)
  26. self.drawBrushes(qp)
  27. qp.end()
  28. def drawBrushes(self, qp):
  29. brush = QBrush(Qt.SolidPattern)
  30. qp.setBrush(brush)
  31. qp.drawRect(10, 15, 90, 60)
  32. brush.setStyle(Qt.Dense1Pattern)
  33. qp.setBrush(brush)
  34. qp.drawRect(130, 15, 90, 60)
  35. brush.setStyle(Qt.Dense2Pattern)
  36. qp.setBrush(brush)
  37. qp.drawRect(250, 15, 90, 60)
  38. brush.setStyle(Qt.DiagCrossPattern)
  39. qp.setBrush(brush)
  40. qp.drawRect(10, 105, 90, 60)
  41. brush.setStyle(Qt.Dense5Pattern)
  42. qp.setBrush(brush)
  43. qp.drawRect(130, 105, 90, 60)
  44. brush.setStyle(Qt.Dense6Pattern)
  45. qp.setBrush(brush)
  46. qp.drawRect(250, 105, 90, 60)
  47. brush.setStyle(Qt.HorPattern)
  48. qp.setBrush(brush)
  49. qp.drawRect(10, 195, 90, 60)
  50. brush.setStyle(Qt.VerPattern)
  51. qp.setBrush(brush)
  52. qp.drawRect(130, 195, 90, 60)
  53. brush.setStyle(Qt.BDiagPattern)
  54. qp.setBrush(brush)
  55. qp.drawRect(250, 195, 90, 60)
  56. if __name__ == '__main__':
  57. app = QApplication(sys.argv)
  58. ex = Example()
  59. sys.exit(app.exec_())

在我们的示例中,我们绘制了九个不同的矩形。

  1. brush = QBrush(Qt.SolidPattern)
  2. qp.setBrush(brush)
  3. qp.drawRect(10, 15, 90, 60)

我们定义一个笔刷对象。 我们将其设置为绘画器对象,并通过调用drawRect()方法绘制矩形。

PyQt5 中的绘图 - 图5

图:笔刷

贝塞尔曲线

贝塞尔曲线是一条三次曲线。 可以使用QPainterPath创建 PyQt5 中的贝塞尔曲线。 画家路径是由许多图形构造块(例如矩形,椭圆形,直线和曲线)组成的对象。

beziercurve.py

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. This program draws a Bézier curve with
  6. QPainterPath.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import QWidget, QApplication
  12. from PyQt5.QtGui import QPainter, QPainterPath
  13. from PyQt5.QtCore import Qt
  14. import sys
  15. class Example(QWidget):
  16. def __init__(self):
  17. super().__init__()
  18. self.initUI()
  19. def initUI(self):
  20. self.setGeometry(300, 300, 380, 250)
  21. self.setWindowTitle('Bézier curve')
  22. self.show()
  23. def paintEvent(self, e):
  24. qp = QPainter()
  25. qp.begin(self)
  26. qp.setRenderHint(QPainter.Antialiasing)
  27. self.drawBezierCurve(qp)
  28. qp.end()
  29. def drawBezierCurve(self, qp):
  30. path = QPainterPath()
  31. path.moveTo(30, 30)
  32. path.cubicTo(30, 30, 200, 350, 350, 30)
  33. qp.drawPath(path)
  34. if __name__ == '__main__':
  35. app = QApplication(sys.argv)
  36. ex = Example()
  37. sys.exit(app.exec_())

本示例绘制了贝塞尔曲线。

  1. path = QPainterPath()
  2. path.moveTo(30, 30)
  3. path.cubicTo(30, 30, 200, 350, 350, 30)

我们使用QPainterPath路径创建贝塞尔曲线。 曲线是通过cubicTo()方法创建的,该方法需要三个点:起点,控制点和终点。

  1. qp.drawPath(path)

使用drawPath()方法绘制最终路径。

PyQt5 中的绘图 - 图6

图:贝塞尔曲线

在 PyQt5 教程的这一部分中,我们做了一些基本的绘图。