方法
画文字
-- coding: utf-8 --
"""
【简介】
在窗体中绘画出文字的例子
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt
class Drawing(QWidget):
def init(self, parent=None):
super(Drawing, self).init(parent)
self.setWindowTitle("在窗体中绘画出文字例子")
self.resize(300, 200)
self.text = '欢迎学习 PyQt5'
def paintEvent(self, event):
painter = QPainter(self)
painter.begin(self)
# 自定义的绘画方法
self.drawText(event, painter)
painter.end()
def drawText(self, event, qp):
# 设置笔的颜色
qp.setPen(QColor(168, 34, 3))
# 设置字体
qp.setFont(QFont('SimSun', 20))
# 画出文本
qp.drawText(event.rect(), Qt.AlignCenter, self.text)
if name == "main":
app = QApplication(sys.argv)
demo = Drawing()
demo.show()
sys.exit(app.exec_())
画点
-- coding: utf-8 --
"""
【简介】
在窗体中绘画点的例子
"""
import sys, math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
class Drawing(QWidget):
def init(self, parent=None):
super(Drawing, self).init(parent)
self.resize(300, 200)
self.setWindowTitle("在窗体中画点")
def paintEvent(self, event):
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):
# [-100, 100]两个周期的正弦函数图像
x = 100 * (-1 + 2.0 * i / 1000) + size.width() / 2.0
y = -50 * math.sin((x - size.width() / 2.0) * math.pi / 50) + size.height() / 2.0
qp.drawPoint(x, y)
if name == 'main':
app = QApplication(sys.argv)
demo = Drawing()
demo.show()
sys.exit(app.exec_())
Pen
-- coding: utf-8 --
"""
【简介】
绘图中QPen 的例子 ,绘制使用不同样式的6条线
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
class Drawing(QWidget):
def init(self):
super().init()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 280, 270)
self.setWindowTitle('钢笔样式例子')
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)
demo = Drawing()
demo.show()
sys.exit(app.exec_())
brush
-- coding: utf-8 --
"""
【简介】
绘图中QBrush 的例子 ,绘制九个不同样式的矩形。
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
class Drawing(QWidget):
def init(self):
super().init()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 365, 280)
self.setWindowTitle('画刷例子')
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawLines(qp)
qp.end()
def drawLines(self, qp):
brush = QBrush(Qt.SolidPattern)
qp.setBrush(brush)
qp.drawRect(10, 15, 90, 60)
brush = QBrush(Qt.Dense1Pattern)
qp.setBrush(brush)
qp.drawRect(130, 15, 90, 60)
brush = QBrush(Qt.Dense2Pattern)
qp.setBrush(brush)
qp.drawRect(250, 15, 90, 60)
brush = QBrush(Qt.Dense3Pattern)
qp.setBrush(brush)
qp.drawRect(10, 105, 90, 60)
brush = QBrush(Qt.DiagCrossPattern)
qp.setBrush(brush)
qp.drawRect(10, 105, 90, 60)
brush = QBrush(Qt.Dense5Pattern)
qp.setBrush(brush)
qp.drawRect(130, 105, 90, 60)
brush = QBrush(Qt.Dense6Pattern)
qp.setBrush(brush)
qp.drawRect(250, 105, 90, 60)
brush = QBrush(Qt.HorPattern)
qp.setBrush(brush)
qp.drawRect(10, 195, 90, 60)
brush = QBrush(Qt.VerPattern)
qp.setBrush(brush)
qp.drawRect(130, 195, 90, 60)
brush = QBrush(Qt.BDiagPattern)
qp.setBrush(brush)
qp.drawRect(250, 195, 90, 60)
if name == 'main':
app = QApplication(sys.argv)
demo = Drawing()
demo.show()
sys.exit(app.exec_())