赞
踩
(本系列中所有代码在windows7 64位[]/Python 3.4.3 32bit/PyQt GPL v5.5 for Python v3.4 (x32)/eric6-6.0.8下测试通过.)
原本地址:http://zetcode.com/gui/pyqt5/
================================================================================
PyQt5的绘画系统可以渲染矢量图形、图片和字符型文本轮廓.当我们想要改变想增强一个现有的部件,或者我们从头创建一个自定义部件的时候,可能就需要绘画.要绘画的话,我们需要PyQt5工具包所提供的绘画API.
绘画是通过painEvent()方法完成的.绘画代码是在QPainter对象的begin()和end()方法之间.它是在部件和其他绘画设备上执行底层绘画.
我们在一个客户端窗口区域绘制一些统一标准字符文本.
#!/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): ...绘画是通过绘画事件完成的.
qp = QPainter() qp.begin(self) self.drawText(event, qp) qp.end()QPainter类是负责所有底层的绘制工作.所有的绘制方法都是在begin()和end()之间.实际的绘制是drawText()方法完成的.
qp.setPen(QColor(168, 34, 3)) qp.setFont(QFont('Decorative', 10))这里我们定义了一个用于绘制文本的笔和字体.
qp.drawText(event.rect(), Qt.AlignCenter, self.text)drawText()方法绘制文本到窗口上.绘制事件的rect()方法返回需要更新的矩形.
图片:绘制文本
点是可以绘制的最简单的图形对象.它是窗口上的小点.
#!/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_())
qp.setPen(Qt.red)我们设置笔为红色.使用了预定义的Qt.red颜色常量.
size = self.size()每一次我们调整窗口大小,一个绘制事件就会生成.我们通过size()方法得到当前窗口大小.我们用窗口的大小去分派在窗口中的点.
qp.drawPoint(x, y)我们通过drawPoint()方法来绘制点.
图片:绘制点
颜色是一个对象,它由红、绿和蓝(RGB)三种灰度值综合而成.可法的RGB值是从0到255.我们可以通过多种方式去定义颜色.最常用的RGB是十进制或十六进制.我们还可以用一个RGBA值,它分别代表了红、绿、蓝和通明度.在这里我们添加了透明度的信息.透明度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')在这里我们用一个十六进制符号定义了一个颜色.
qp.setBrush(QColor(200, 0, 0)) qp.drawRect(10, 15, 90, 60)在这里我们定义了一个刷子并绘制了一个矩形.一个brush是基本图形对象,它用于绘制开关的背景色.drawRect()方法接收4个参数.前两上是坐标轴的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_())
pen = QPen(Qt.black, 2, Qt.SolidLine)我们创建了一个QPen对象.颜色是黑色.宽度设置为2个像素,所以我们能看到它和别的类型笔的不同.Qt.SolidLine是预定义的一种类型笔.
pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen)在这里我们自定义了一种类型笔.我们设置Qt.CustomDashLine类型笔并调用setDashPatern()类型.列表的数量定义了一种类型.这里必须有偶数个数字.奇数数字定义一个破折号,偶数数字定义空格.数字越大,破折号或空格就越大.我们的类型是一个像素的破折号,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_())
brush = QBrush(Qt.SolidPattern) qp.setBrush(brush) qp.drawRect(10, 15, 90, 60)我们定义了一个刷子对象.我们在painter对象里设置它,并通过调用drawRect()方法来绘制矩形.
图片:刷子
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。