参考https://www.cnblogs.com/jgg54335/p/14904853.html
实例一:
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()
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_())
实例二:
import sys, math
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
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_())
实例三:
标签:__,qp,self,PyQT5,PyQt5,import,Drawing,QPainter From: https://www.cnblogs.com/jackchen28/p/18246442