原文: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()
方法繪製矩形。
在這部分教程中咱們學習了一些基本的圖形繪製。