PyQt5绘图API

PyQt5绘图API大全
1、绘图API:绘制文本
#1.文本 2.各种图形 3.图像
#QPainter painter=QPainter() painter.begin() painter.end() painter.drawtext() 必须在painevent事件方法中绘制各种元素

import sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter,QColor,QFont
class Drawtext(QWidget):
def __init__(self):
super(Drawtext,self).__init__()
self.setWindowTitle("在窗口上面绘制文直线文本")
self.resize(500,200)
self.text="Python从菜鸟到高手"

def paintEvent(self,event):
painter=QPainter(self) #定义一个画布
painter.begin(self) #初始化画布属性
print("绘图函数已经调用")
painter.setPen(QColor(150,43,5)) #设置画笔的属性颜色
painter.setFont(QFont("SimSun",25)) #设置绘画显示字体

painter.drawText(event.rect(),Qt.AlignCenter,self.text) #设置绘画区域范围
painter.end()
if __name__=="__main__":
app=QApplication(sys.argv)
p=Drawtext()
p.show()
sys.exit(app.exec_())

2、绘制各种图形(圆,矩形,多边形,各种函数图形,椭圆,圆,弧线等)
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Drawall(QWidget):
def __init__(self):
super(Drawall,self).__init__()
self.setWindowTitle("绘制各种图形")
self.resize(300,600)
def paintEvent(self,event):
painter=QPainter(self) #先定义画布
painter.begin(self) #初始化画布

painter.setPen(Qt.blue)

#绘制弧线
rect=QRect(0,10,100,100) #确定画出圆弧的正方形区域(x左上角横坐标,y左上角纵坐标,length矩形长,width矩形宽)
#alen参数:1个alen长度等于1/16度
painter.drawArc(rect,0,90*16) #绘制圆弧的角度范围(矩形区域,起始角度,终止角度)
#通过弧线来绘制圆
painter.setPen(Qt.red)
painter.drawArc(120,10,100,100,0,360*16) #圆的半径大小可以通过设置矩形区域的长宽大小来进行设置
#绘制带弦的弧线
painter.drawChord(10,120,100,100,12,130*16)
# 绘制扇形
painter.drawPie(10,240,100,100,12,130*16)
#绘制椭圆
painter.drawEllipse(120,120,100,150)

#绘制多边形(定义多边形的顶点坐标)
point1=QPoint(140,380)
point2=QPoint(270,420)
point3=QPoint(290,512)
point4=QPoint(290,588)
point5=QPoint(200,533)
polygon=QPolygon([point1,point2,point3,point4,point5])
painter.drawPolygon(polygon)

#绘制图像
image=QImage("./image-1/1-1.jpg")
rect=QRect(10,400,image.width()/15,image.height()/15) #在某个定义好的区域画出图像大小
painter.drawImage(rect,image) #绘制图像函数参数设置
painter.end()

if __name__=="__main__":
app=QApplication(sys.argv)
p=Drawall()
p.show()
sys.exit(app.exec_())

3、绘制像素点正弦曲线
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
class Drawpoints(QWidget):
def __init__(self):
super(Drawpoints,self).__init__()
self.setWindowTitle("窗口像素点绘制正弦曲线")
self.resize(300,200)
def paintEvent(self,event):
painter=QPainter(self)

painter.begin(self)
painter.setPen(Qt.blue)
size=self.size()

for i in range(100):
x=100*(-1+2.0*i/100)+size.width()/2.0 #将主窗口的宽度中心作为x周轴原点
y=-50*math.sin((x-size.width()/2.0)*math.pi/50)+size.height()/2.0 #将主窗口的高度中心作为x周轴原点
painter.drawPoint(x,y)

painter.end()

if __name__=="__main__":
app=QApplication(sys.argv)
p=Drawpoints()
p.show()
sys.exit(app.exec_())

4、绘制不同类型的直线,主要是指对于直线的样式的设置,对于pen样式的设置
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
class Drawmultiline(QWidget):
def __init__(self):
super(Drawmultiline,self).__init__()
self.setWindowTitle("设置pen的样式")
self.resize(300,300)
def paintEvent(self,event):
painter=QPainter(self)

painter.begin(self)
pen=QPen(Qt.red,2,Qt.SolidLine) #设置实线样式直线
painter.setPen(pen)
painter.drawLine(20,40,250,40)

pen.setStyle(Qt.DashLine) #设置宽虚线样式直线
painter.setPen(pen)
painter.drawLine(20, 80, 250, 80)

pen.setStyle(Qt.DashDotDotLine) # 设置双点划线线样式直线
painter.setPen(pen)
painter.drawLine(20, 120, 250, 120)

pen.setStyle(Qt.DashDotLine) # 设置单点划线线样式直线
painter.setPen(pen)
painter.drawLine(20, 160, 250, 160)

pen.setStyle(Qt.DotLine) # 设置窄虚线样式直线
painter.setPen(pen)
painter.drawLine(20, 200, 250, 200)

pen.setStyle(Qt.CustomDashLine) # 自定义样式直线,可以使用列表中数值比例来进行设置
pen.setDashPattern([1,10,5,4])
painter.setPen(pen)
painter.drawLine(20, 240, 250, 240)

size=self.size()
painter.end()

if __name__=="__main__":
app=QApplication(sys.argv)
p=Drawmultiline()
p.show()
sys.exit(app.exec_())

5、用画刷填充区域
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Fillrect(QWidget):
def __init__(self):
super(Fillrect,self).__init__()
self.setWindowTitle("用画刷填充区域")
self.resize(900,600)



def paintEvent(self,event):
painter=QPainter(self) #先定义画布
painter.begin(self) #初始化画布
brush=QBrush(Qt.SolidPattern) #定义实心画刷样式
painter.setBrush(brush)
painter.drawRect(10,15,90,60)

brush = QBrush(Qt.Dense1Pattern) # 定义实心画刷样式1
painter.setBrush(brush)
painter.drawRect(120, 15, 90, 60)

brush = QBrush(Qt.Dense2Pattern) # 定义实心画刷样式2
painter.setBrush(brush)
painter.drawRect(230, 15, 90, 60)

brush = QBrush(Qt.Dense3Pattern) # 定义实心画刷样式3
painter.setBrush(brush)
painter.drawRect(340, 15, 90, 60)
brush = QBrush(Qt.Dense4Pattern) # 定义实心画刷样式4
painter.setBrush(brush)
painter.drawRect(450, 15, 90, 60)
brush = QBrush(Qt.Dense5Pattern) # 定义实心画刷样式5
painter.setBrush(brush)
painter.drawRect(560, 15, 90, 60)
brush = QBrush(Qt.Dense6Pattern) # 定义实心画刷样式6
painter.setBrush(brush)
painter.drawRect(670, 15, 90, 60)
brush = QBrush(Qt.Dense7Pattern) # 定义实心画刷样式7
painter.setBrush(brush)
painter.drawRect(780, 15, 90, 60)
painter.end()


if __name__=="__main__":
app=QApplication(sys.argv)
p=Fillrect()
p.show()
sys.exit(app.exec_())

posted @ 2020-02-11 09:21  The-Chosen-One  阅读(2811)  评论(0编辑  收藏  举报