PyQt5-绘制各种图形
"""
绘制各种图形
弧
圆形
矩形(正方形)
多边形
虚线画图形
渐变色图形
渐变色字体
绘制图像
"""
import sys, math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import (QPen, QPainter, QBrush, QLinearGradient, QConicalGradient, QRadialGradient)
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QFrame, QSizePolicy)
class DrawAll(QWidget):
def __init__(self):
super(DrawAll, self).__init__()
self.resize(900, 600)
self.setWindowTitle('绘制各种图形—By:小丑不冷')
def paintEvent(self, event):
qp = QPainter()
pen = QPen()
qp.begin(self)
x, y, w, h, number = 0, 0, 900, 0, 6
qp.setPen(QPen(Qt.black, 2, Qt.DashLine))
qp.drawLine(150, 0, 150, 600)
qp.drawLine(300, 0, 300, 600)
qp.drawLine(450, 0, 450, 600)
qp.drawLine(600, 0, 600, 600)
qp.drawLine(900, 0, 900, 600)
while number > 1:
qp.drawLine(x, y, w, h)
w = 600
y += 150
h += 150
number -= 1
if number == 2:
w += 300
pen.setWidth(4)
pen.setColor(Qt.blue)
qp.setPen(pen)
rect = QRect(30, 25, 100, 100)
qp.drawArc(rect, 90 * 16, 90 * 16)
pen.setColor(Qt.red)
qp.setPen(pen)
qp.drawArc(180, 25, 100, 100, 0 * 16, 360 * 16)
pen.setColor(Qt.yellow)
qp.setPen(pen)
qp.drawArc(10, 200, 130, 50, 0 * 16, 360 * 16)
pen.setColor(Qt.green)
qp.setPen(pen)
qp.drawChord(180, 200, 100, 50, 0 * 16, 180 * 16)
pen.setColor(Qt.gray)
qp.setPen(pen)
qp.drawPie(0, 350, 150, 150, 45 * 16, 90 * 16)
pen.setColor(Qt.magenta)
qp.setPen(pen)
"""
坐标需要精确计算,以point1为初始点
point2坐标连接point1,point3坐标连接point2
以此类推,最后point6连接point1
"""
point1 = QPoint(200, 325)
point2 = QPoint(260, 325)
point3 = QPoint(285, 375)
point4 = QPoint(260, 425)
point5 = QPoint(200, 425)
point6 = QPoint(175, 375)
polygon = QPolygon([point1, point2, point3, point4, point5, point6])
qp.drawPolygon(polygon)
pen = QColor(0, 0, 0)
pen.setNamedColor('#7300FF')
qp.setPen(QPen(pen, 3, Qt.DashLine))
qp.drawArc(20, 480, 100, 100, 0, 360 * 16)
while True:
for Arc_x in range(2, 5):
qp.drawArc(Arc_x * 17, Arc_x * 10 + 480, Arc_x * 10, Arc_x * 10, 0, 360 * 16)
break
pen = QColor(0, 0, 0)
pen.setNamedColor('#ff8b02')
qp.setPen(QPen(pen, 3, Qt.DashDotLine))
point1 = QPoint(170, 490)
point2 = QPoint(280, 490)
point3 = QPoint(280, 560)
point4 = QPoint(170, 560)
polygon = QPolygon([point1, point2, point3, point4])
qp.drawPolygon(polygon)
pen = QLinearGradient(QPointF(300, 10), QPointF(300, 100))
pen.setColorAt(0, Qt.magenta)
pen.setColorAt(1, Qt.green)
qp.setPen(QPen(pen, 5, Qt.SolidLine))
qp.drawLine(335, 20, 335, 130)
qp.setPen(QPen(pen, 5, Qt.DashLine))
qp.drawLine(355, 20, 355, 140)
qp.setPen(QPen(pen, 5, Qt.DashDotLine))
qp.drawLine(375, 20, 375, 140)
qp.setPen(QPen(pen, 5, Qt.DotLine))
qp.drawLine(395, 20, 395, 140)
qp.setPen(QPen(pen, 5, Qt.DashDotDotLine))
qp.drawLine(415, 20, 415, 140)
pen = QLinearGradient(QPointF(350, 10), QPointF(350, 100))
pen.setColorAt(0, Qt.red)
pen.setColorAt(1, Qt.blue)
qp.setFont(QFont('SimSun', 20))
qp.setBrush(pen)
qp.drawText(480, 70, "测试字体")
image = QImage(r'C:\Users\点雨洛\Desktop\小人.png')
rect = QRect(600, 0, image.width()/3.2, image.height()/3.95)
qp.drawImage(rect, image)
qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = DrawAll()
main.show()
sys.exit(app.exec_())
