原文:http://zetcode.com/gui/pyqt5/painting/python
PyQt5的绘图系统可用于渲染矢量图、图像和文本。若是想改变或加强已有的控件,或者想从头建立一个自定义控件时,咱们就须要在程序中进行图形的绘制。咱们可使用PyQt5提供的绘图API进行绘图操做。web
绘图要在paintEvent()
方法中实现。在QPainter
对象的begin()
与end()
方法间编写绘图代码。它会在控件或其余图形设备上进行低级的图形绘制。app
咱们先以窗体内Unicode文本的绘制为例。dom
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we draw text in Russian azbuka. author: Jan Bodnar website: zetcode.com last edited: September 2015 """ import sys from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QColor, QFont from PyQt5.QtCore import Qt class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\ \u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\ \u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430' self.setGeometry(300, 300, 280, 170) self.setWindowTitle('Draw text') self.show() 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) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
示例中咱们绘制了一些西里尔字母,文本是垂直且水平对齐的。学习
def paintEvent(self, event): ...
绘制工做在paintEvent的方法内部完成。字体
qp = QPainter() qp.begin(self) self.drawText(event, qp) qp.end()
QPainter
负责全部的低级绘制工做,在它的begin()与end()间放置了绘图代码。实际的绘制工做由drawText()
方法完成。ui
qp.setPen(QColor(168, 34, 3)) qp.setFont(QFont('Decorative', 10))
这里咱们定义了用于绘制文本的画笔与字体对象。this
qp.drawText(event.rect(), Qt.AlignCenter, self.text)
drawText()
会在窗体上进行文本的绘制。经过paint event(绘图事件)的rect()
方法获得当前窗体的可绘图区域。3d
点是能够绘制的最简单的图形对象。code
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In the example, we draw randomly 1000 red points on the window. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys, random from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QColor, QPen from PyQt5.QtCore import Qt class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 280, 170) self.setWindowTitle('Points') self.show() 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() for i in range(1000): x = random.randint(1, size.width()-1) y = random.randint(1, size.height()-1) qp.drawPoint(x, y) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
示例中咱们随机画了1000个红点。
qp.setPen(Qt.red)
将画笔设为红色。咱们使用了预约义的Qt.red常量
size = self.size()
每次调整窗体尺寸都会生成一个paint event。咱们能够经过这个event的size()方法获得窗体当前的尺寸。咱们将这些点分配到窗体的各个区域。
qp.drawPoint(x, y)
经过drawPoint()
方法绘制圆点。
颜色是用于表示红绿蓝(RGB)各色值组合体的对象。合法的RGB值在0到255之间。颜色的定义有多种方式,一般用10进制或16进制的数值表示。咱们也可使用RGBA表示,它表明了红色、绿色、蓝色与Alpha通道值。也就是说咱们附加了一个表示透明度的信息。Alpha值为255表示彻底不透明,为0表示彻底透明,也就是说颜色是不可见的。
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example draws three rectangles in three #different colours. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QColor, QBrush class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 350, 100) self.setWindowTitle('Colours') self.show() 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) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
示例中咱们绘制了三个不一样颜色的矩形。
color = QColor(0, 0, 0) color.setNamedColor('#d4d4d4')
这里咱们使用16进制值定义了一个颜色对象。
qp.setBrush(QColor(200, 0, 0)) qp.drawRect(10, 15, 90, 60)
咱们为QPainter设置了一个笔刷(Bursh)对象并用它绘制了一个矩形。笔刷是用于绘制形状背景的基本图形对象。drawRect()
方法接受四个参数,前两个是起点的x,y坐标,后两个是矩形的宽和高。这个方法使用当前的画笔与笔刷对象进行绘制。
QPen是一个基本图形对象。它用于绘制直线,曲线以及矩形、椭圆、多边形或其余图形的轮廓。
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example we draw 6 lines using different pen styles. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QColor, QPen from PyQt5.QtCore import Qt class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 280, 270) self.setWindowTitle('Pen styles') self.show() 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) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
示例中咱们绘制了6条直线。每条直线使用了不一样的画笔风格,其中有5个是PyQt5中预约义的,另外咱们也自已实现了一个。最后那条线就是用咱们自定义的画笔风格所画。
pen = QPen(Qt.black, 2, Qt.SolidLine)
咱们建立了一个黑颜色的画笔对象,其宽度为2像素,这样能够看出画笔风格的不一样之处。Qt.SolidLine
是预约义的一种画笔风格。
pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen)
这里咱们定义了一个画笔风格。咱们设置了Qt.CustomDashLine并调用了setDashPattern()
方法,它的参数(一个数字列表)定义了一种风格,必须有偶数个数字;其中奇数表示绘制实线,偶数表示留空。数值越大,直线或空白就越大。这里咱们定义了1像素的实线,4像素的空白,5像素实线,4像素空白。。。
QBrush是一个基本图形对象。它用于绘制矩形、椭圆或多边形等图形的背景。笔刷有三种类型:预约义笔刷、渐变笔刷或纹理图案笔刷。
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example draws 9 rectangles in different brush styles. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QBrush from PyQt5.QtCore import Qt class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 355, 280) self.setWindowTitle('Brushes') self.show() 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.Dense3Pattern) qp.setBrush(brush) qp.drawRect(10, 105, 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) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
在示例中咱们绘制了9个不一样的矩形。
brush = QBrush(Qt.SolidPattern) qp.setBrush(brush) qp.drawRect(10, 15, 90, 60)
咱们定义了一个笔刷对象,而后将它设置给QPainter对象,并调用painter的drawRect()
方法绘制矩形。
在这部分教程中咱们学习了一些基本的图形绘制。