绘制不同类型的直线
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/6/17 20:01
# @Author : kevin
# @Site :
# @File : 绘制不同类型的直线.py
# @Software: PyCharm
"""
绘制不同类型的直线
"""
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.resize(400, 300) # 设置主窗口尺寸
self.setWindowTitle('设置Pen的样式') # 设置主窗口标题
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
pen = QPen(Qt.red, 3, Qt.SolidLine) # 实线____________
painter.setPen(pen)
painter.drawLine(40, 40, 350, 40) # 40, 40, 350, 40 在resize中,起始横坐标, 起始纵坐标,结束横坐标,结束纵坐标
# -----------------------下面两行可以没有,没有的话就不写字---------------------------------------------
painter.setFont(QFont('SimSun', 15)) # 设置字体,字号
painter.drawText(40, 20, 320, 20, 100, '这是实线') #
# ------------------------------------------------------------------------------------------------------
pen = QPen(Qt.black, 3, Qt.DashLine) # 虚线——————
painter.setPen(pen)
painter.drawLine(40, 80, 350, 80) # 40, 80, 350, 80 在resize中,起始横坐标, 起始纵坐标,结束横坐标,结束纵坐标
# -----------------------下面两行可以没有,没有的话就不写字---------------------------------------------
painter.setFont(QFont('SimSun', 15)) # 设置字体,字号
painter.drawText(40, 40, 320, 40, 100, '这是虚线') #
# ------------------------------------------------------------------------------------------------------
pen = QPen(Qt.green, 3, Qt.DashDotLine) # 点画线—.—.—.
painter.setPen(pen)
painter.drawLine(40, 120, 350, 120) # 40, 120, 350, 120 在resize中,起始横坐标, 起始纵坐标,结束横坐标,结束纵坐标
# -----------------------下面两行可以没有,没有的话就不写字---------------------------------------------
painter.setFont(QFont('SimSun', 15)) # 设置字体,字号
painter.drawText(40, 60, 320, 60, 100, '这是点画线') #
# ------------------------------------------------------------------------------------------------------
pen = QPen(Qt.yellow, 3, Qt.DotLine) # 密集虚线 ......
painter.setPen(pen)
painter.drawLine(40, 160, 350, 160) # 40, 160, 350, 160 在resize中,起始横坐标, 起始纵坐标,结束横坐标,结束纵坐标
# -----------------------下面两行可以没有,没有的话就不写字---------------------------------------------
painter.setFont(QFont('SimSun', 15)) # 设置字体,字号
painter.drawText(40, 80, 320, 80, 100, '这是密集虚线') #
# ------------------------------------------------------------------------------------------------------
pen = QPen(Qt.gray, 3, Qt.DashDotDotLine) # 点点线 —..—..—
painter.setPen(pen)
painter.drawLine(40, 200, 350, 200) # 40, 200, 350, 200 在resize中,起始横坐标, 起始纵坐标,结束横坐标,结束纵坐标
# -----------------------下面两行可以没有,没有的话就不写字---------------------------------------------
painter.setFont(QFont('SimSun', 15)) # 设置字体,字号
painter.drawText(40, 100, 320, 100, 100, '这是点点线') #
# ------------------------------------------------------------------------------------------------------
size = self.size()
painter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = DrawMultiLine()
main.show()
sys.exit(app.exec_())