pyqt5教程11:绘制外观
Painting in PyQt5
PyQt5 繪畫系統(tǒng)能夠渲染矢量圖形、圖像和基于輪廓字體的文本。當我們想要更改或增強現(xiàn)有小部件時,或者如果我們從頭開始創(chuàng)建自定義小部件時,應用程序中需要繪畫。為了進行繪圖,我們使用 PyQt5 工具包提供的繪圖 API。
QPainter
QPainter 在小部件和其他繪畫設備上執(zhí)行低級繪畫。它可以繪制從簡單線條到復雜形狀的所有內(nèi)容。
The paintEvent method
繪畫是在paintEvent 方法中完成的。繪畫代碼位于 QPainter 對象的開始和結束方法之間。它在小部件和其他繪畫設備上執(zhí)行低級繪畫。
PyQt5 draw text
我們首先在窗口的客戶區(qū)繪制一些 Unicode 文本。
draw_text.py
#!/usr/bin/python""" ZetCode PyQt5 tutorialIn this example, we draw text in Russian Cylliric.Author: Jan Bodnar Website: zetcode.com """import sys from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QColor, QFont from PyQt5.QtCore import Qtclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.text = "Лев Николаевич Толстой\nАнна Каренина"self.setGeometry(300, 300, 350, 300)self.setWindowTitle('Drawing 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)def main():app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())if __name__ == '__main__':main()在我們的示例中,我們用 Cylliric 繪制了一些文本。文本垂直和水平對齊。
def paintEvent(self, event): ...Drawing is done within the paint event.
qp = QPainter() qp.begin(self) self.drawText(event, qp) qp.end()QPainter 類負責所有低級繪畫。所有的繪畫方法都在開始方法和結束方法之間進行。實際的繪畫被委托給 drawText 方法。
qp.setPen(QColor(168, 34, 3)) qp.setFont(QFont('Decorative', 10))在這里,我們定義了用于繪制文本的筆和字體。
qp.drawText(event.rect(), Qt.AlignCenter, self.text)drawText 方法在窗口上繪制文本。繪制事件的 rect 方法返回需要更新的矩形。使用 Qt.AlignCenter,我們可以在兩個維度上對齊文本。
Figure: Drawing text
PyQt5 draw points
點是可以繪制的最簡單的圖形對象。這是窗戶上的一個小地方。
draw_points.py
#!/usr/bin/python""" ZetCode PyQt5 tutorialIn the example, we draw randomly 1000 red points on the window.Author: Jan Bodnar Website: zetcode.com """from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter from PyQt5.QtCore import Qt import sys, randomclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setGeometry(300, 300, 300, 190)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()if size.height() <= 1 or size.height() <= 1:returnfor 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()在我們的示例中,我們在窗口的客戶區(qū)隨機繪制 1000 個紅點。
qp.setPen(Qt.red)我們將筆設置為紅色。我們使用預定義的 Qt.red 顏色常數(shù)。
size = self.size()每次我們調(diào)整窗口大小時,都會生成一個繪制事件。我們使用 size 方法獲取窗口的當前大小。我們使用窗口的大小將點分布在窗口的客戶區(qū)。
qp.drawPoint(x, y)We draw the point with the?drawPoint?method.
正在上傳…重新上傳取消
Figure: Points
PyQt5 colours
顏色是表示紅色、綠色和藍色 (RGB) 強度值組合的對象。有效的 RGB 值在 0 到 255 的范圍內(nèi)。我們可以通過多種方式定義顏色。最常見的是 RGB 十進制值或十六進制值。我們還可以使用代表紅色、綠色、藍色和 Alpha 的 RGBA 值。在這里,我們添加了一些關于透明度的額外信息。 Alpha 值 255 定義完全不透明度,0 表示完全透明,例如顏色是看不見的。
colours.py
#!/usr/bin/python""" ZetCode PyQt5 tutorialThis example draws three rectangles in three different colours.Author: Jan Bodnar Website: zetcode.com """from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QColor, QBrush import sysclass 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)def main():app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())if __name__ == '__main__':main()在我們的示例中,我們繪制了三個彩色矩形。
color = QColor(0, 0, 0) color.setNamedColor('#d4d4d4')在這里,我們使用十六進制表示法定義顏色。
qp.setBrush(QColor(200, 0, 0)) qp.drawRect(10, 15, 90, 60)這里我們定義一個畫筆并繪制一個矩形。畫筆是用于繪制形狀背景的基本圖形對象。 drawRect 方法接受四個參數(shù)。前兩個是軸上的 x 和 y 值。第三個和第四個參數(shù)是矩形的寬度和高度。該方法使用當前的筆和畫筆繪制矩形。
轉(zhuǎn)存失敗重新上傳取消
Figure: Colours
PyQt5 QPen
QPen 是一個基本的圖形對象。它用于繪制矩形、橢圓、多邊形或其他形狀的直線、曲線和輪廓。
pens.py
#!/usr/bin/python""" ZetCode PyQt5 tutorialIn this example we draw 6 lines using different pen styles.Author: Jan Bodnar Website: zetcode.com """from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QPen from PyQt5.QtCore import Qt import sysclass 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)def main():app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())if __name__ == '__main__':main()在我們的示例中,我們繪制了六條線。這些線條是用六種不同的筆樣式繪制的。有五種預定義的筆樣式。我們還可以創(chuàng)建自定義筆樣式。最后一行是使用自定義筆樣式繪制的。
pen = QPen(Qt.black, 2, Qt.SolidLine)我們創(chuàng)建一個 QPen 對象。顏色是黑色。寬度設置為 2 像素,以便我們可以看到筆樣式之間的差異。 Qt.SolidLine 是預定義的筆樣式之一。
pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen)這里我們定義了一個自定義的筆樣式。我們設置 Qt.CustomDashLine 筆樣式并調(diào)用 setDashPattern 方法。數(shù)字列表定義了一種風格。必須有偶數(shù)個數(shù)。奇數(shù)定義一個破折號,偶數(shù)空間。數(shù)字越大,空格或破折號越大。我們的模式是 1 px dash、4 px space、5 px dash、4 px space 等。
Figure: Pen styles
PyQt5 QBrush
QBrush 是一個基本的圖形對象。它用于繪制圖形形狀的背景,例如矩形、橢圓或多邊形。畫筆可以是三種不同的類型:預定義畫筆、漸變或紋理圖案。
brushes.py
#!/usr/bin/python""" ZetCode PyQt5 tutorialThis example draws nine rectangles in different brush styles.Author: Jan Bodnar Website: zetcode.com """from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QBrush from PyQt5.QtCore import Qt import sysclass 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.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()在我們的示例中,我們繪制了九個不同的矩形。
brush = QBrush(Qt.SolidPattern) qp.setBrush(brush) qp.drawRect(10, 15, 90, 60)我們定義一個畫筆對象。我們將其設置為畫家對象并通過調(diào)用 drawRect 方法繪制矩形。
正在上傳…重新上傳取消
Figure: Brushes
Bézier curve
貝塞爾曲線是一條三次線。 PyQt5 中的貝塞爾曲線可以使用 QPainterPath 創(chuàng)建。畫家路徑是由許多圖形構建塊組成的對象,例如矩形、橢圓、直線和曲線。
bezier_curve.py
#!/usr/bin/python""" ZetCode PyQt5 tutorialThis program draws a Bézier curve with QPainterPath.Author: Jan Bodnar Website: zetcode.com """import sysfrom PyQt5.QtGui import QPainter, QPainterPath from PyQt5.QtWidgets import QWidget, QApplicationclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setGeometry(300, 300, 380, 250)self.setWindowTitle('Bézier curve')self.show()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()This example draws a Bézier curve.
path = QPainterPath() path.moveTo(30, 30) path.cubicTo(30, 30, 200, 350, 350, 30)我們使用 QPainterPath 路徑創(chuàng)建貝塞爾曲線。曲線是使用cubicTo 方法創(chuàng)建的,該方法需要三個點:起點、控制點和終點。
qp.drawPath(path)最終路徑是使用 drawPath 方法繪制的。
轉(zhuǎn)存失敗重新上傳取消
Figure: Bézier curve
總結
以上是生活随笔為你收集整理的pyqt5教程11:绘制外观的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pyqt5教程10:Widgets2组件
- 下一篇: pyqt5教程12:拖放功能