本篇 ShengYu 介紹 Python PyQt5 QPainter 用法與範例。
以下的 Python PyQt5 QPainter 用法與範例將分為這幾部分,
- 建立 PyQt5 QPainter
- PyQt5 QPainter drawText 繪製文字
- PyQt5 QPainter drawLine 繪製直線
- PyQt5 QPainter drawRect 繪製矩形
- PyQt5 QPainter drawEllipse 繪製橢圓
- PyQt5 QPainter drawArc 繪製圓弧、圓形
- PyQt5 QPainter drawPolygon 繪製多邊形
- PyQt5 QPainter drawImage 繪製影像
那我們開始吧!
建立 PyQt5 QPainter
PyQt5 簡單建立 QPainter 的用法如下,繪製的操作會在 QWidget.paintEvent()
函式裡完成,並且繪製的函式必須放在 QPainter.begin()
與 QPainter.end()
之間,這些繪圖函式稍後會介紹,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
# paint ...
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
如果要改變背景顏色的話可以使用 palette,例如將背景調整成白色或黑色,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(self.backgroundRole(), Qt.white)
# palette.setColor(self.backgroundRole(), Qt.black)
self.setPalette(palette)
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
# paint ...
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
PyQt5 QPainter drawText 繪製文字
PyQt5 QPainter 繪製文字 drawText 函式,在使用之前先設定一下畫筆與字型,QPainter.setPen()
可以設定畫筆顏色,你可以透過 QColor 來指定 RGB 的顏色,或者使用 PyQt5 內建提供的常用顏色,例如:Qt.black 黑色、Qt.white 白色、Qt.red 紅色、Qt.green 綠色、Qt.blue 藍色等等,QPainter.setFont()
是設定字型,第二個參數為字型大小,
QPainter.drawText()
繪製文字時是使用傳入的 (x, y) 座標作為文字的左上角座標,而不是文字中心的座標,QPainter.drawText()
繪製文字也可以傳入 QRect 的方式 x, y 分別為文字的左上角座標與 w, h 寬度跟高度,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt, QPoint, QRect
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
qpainter.setPen(QColor(0, 0, 255))
qpainter.setFont(QFont('Arial', 20))
qpainter.drawText(QPoint(10, 30), 'PyQt5')
# qpainter.drawText(10, 30, 'PyQt5')
# qpainter.drawText(QRect(10, 30, 100, 30), Qt.AlignLeft, 'PyQt5')
# qpainter.drawText(10, 30, 100, 30, Qt.AlignLeft, 'PyQt5')
qpainter.drawText(event.rect(), Qt.AlignCenter, 'hello world')
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
PyQt5 QPainter drawLine 繪製直線
PyQt5 QPainter drawLine 繪製直線時需要傳入兩組座標 (x1, y1) 與 (x2, y2),繪製直線之前先設定畫筆,建立 QPen()
分別傳入線條顏色,線條寬度,線條樣式,如下例中的 QPen(Qt.black, 2, Qt.SolidLine)
,線條樣式有 Qt.SolidLine
、Qt.DashLine
、Qt.DashDotLine
、Qt.DashDotDotLine
可選,另外還有 Qt.CustomDashLine
選項可以客製化,這邊先不介紹,線條樣式也可以透過 QPen.setStyle()
來改變,改變後記得使用 QPen.setPen()
重新設定一下,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
qpen = QPen(Qt.black, 2, Qt.SolidLine)
qpainter.setPen(qpen)
qpainter.drawLine(20, 40, 180, 40)
qpen.setStyle(Qt.DashLine)
qpainter.setPen(qpen)
qpainter.drawLine(20, 60, 180, 60)
qpen.setStyle(Qt.DashDotLine)
qpainter.setPen(qpen)
qpainter.drawLine(20, 80, 180, 80)
qpen.setStyle(Qt.DashDotDotLine)
qpainter.setPen(qpen)
qpainter.drawLine(20, 100, 180, 100)
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
PyQt5 QPainter drawRect 繪製矩形
PyQt5 QPainter drawRect 繪製矩形時是帶入 QRect,x, y 為矩形的左上角座標 w, h 為矩形的寬度與高度,矩形的顏色可以透過 QPainter.setPen()
來更換,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt, QRect
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
qpainter.setPen(Qt.black)
qpainter.drawRect(QRect(10, 10, 80, 80))
qpainter.setPen(Qt.green)
qpainter.drawRect(100, 10, 80, 80)
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
PyQt5 QPainter drawEllipse 繪製橢圓
PyQt5 QPainter 繪製橢圓時是使用 drawEllipse 函式,也可以用來繪製圓形,傳入 QRect 的 x, y 分別為橢圓外圍/圓形的矩形的左上角座標與 w, h 寬度跟高度,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt, QRect
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
qpen = QPen(Qt.black)
qpainter.setPen(qpen)
qpainter.drawEllipse(QRect(10, 30, 40, 80))
qpainter.drawEllipse(QRect(60, 30, 80, 40))
qpainter.drawEllipse(QRect(150, 30, 40, 40))
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
PyQt5 QPainter drawArc 繪製圓弧、圓形
PyQt5 QPainter 繪製圓弧是使用 drawArc 函式,傳入 QRect 的 x, y 分別為矩形的左上角座標與 w, h 寬度跟高度,第二個參數為起始的角度,第二個參數為結束的角度,這兩個角度需要乘 16,因爲單位爲 alen,一度等於 16 alen,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt, QRect
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
qpen = QPen(Qt.black)
qpainter.setPen(qpen)
qpainter.drawArc(QRect(20, 20, 50, 50), 0, 90*16)
qpainter.drawArc(QRect(100, 20, 50, 50), 0, 180*16)
qpainter.drawArc(QRect(20, 80, 50, 50), 90*16, 180*16)
qpainter.drawArc(QRect(100, 80, 50, 50), 0, 360*16)
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
PyQt5 QPainter drawPolygon 繪製多邊形
PyQt5 QPainter 繪製多邊形是使用 drawPolygon 函式,需要傳入一組座標以上 (x1, y1) 與 (x2, y2) … (xn, yn),可以直接將點群傳入 QPainter.drawPolygon()
函式裡,也可以先建構 QPolygon()
傳入 QPoint list,再把 QPolygon 傳入 QPainter.drawPolygon()
裡,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter, QPen, QPolygon
from PyQt5.QtCore import Qt, QPoint
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
qpen = QPen(Qt.black)
qpainter.setPen(qpen)
p1 = QPoint(40, 40)
p2 = QPoint(60, 20)
p3 = QPoint(80, 40)
p4 = QPoint(80, 80)
p5 = QPoint(40, 80)
qpainter.drawPolygon(p1, p2, p3, p4, p5)
qpen = QPen(Qt.red)
qpainter.setPen(qpen)
p1 = QPoint(100, 40)
p2 = QPoint(120, 20)
p3 = QPoint(140, 40)
p4 = QPoint(140, 80)
p5 = QPoint(100, 80)
qpolygon = QPolygon([p1, p2, p3, p4, p5])
qpainter.drawPolygon(qpolygon)
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
PyQt5 QPainter drawImage 繪製影像
PyQt5 使用 drawImage 來繪製影像,drawImage 第一個參數為 QRect 繪製的區域 x, y 分別為矩形的左上角座標與 w, h 寬度與高度,第二個參數為影像,在這範例中這影像是在 initUI 時就已經先將影像讀取就緒了,在 paintEvent 裡就直接繪製該影像,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import QPainter, QPen, QImage
from PyQt5.QtCore import Qt, QRect
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
self.qimage = QImage('lena.jpg')
def paintEvent(self, event):
qpainter = QPainter()
qpainter.begin(self)
qpainter.drawImage(QRect(10, 10, 120, 120), self.qimage)
qpainter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
以上就是 Python PyQt5 QPainter 用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!