绘图API: 绘制各种图形 的案例
弧形
圆形
椭圆
矩形(正方形)
多边形
import math
import sys
from PyQt5.QtCore import Qt, QRegExp, QDir, QRect, QPoint
from PyQt5.QtGui import QIcon, QFont, QPalette, QPixmap, QIntValidator, QDoubleValidator, QRegExpValidator, QPainter, \
QColor, QPen, QPolygon, QImage
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QToolTip, QHBoxLayout, QMainWindow, QLabel, QVBoxLayout, \
QDialog, QLineEdit, QGridLayout, QFormLayout, QTextEdit, QRadioButton, QCheckBox, QSlider, QSpinBox, QMessageBox, \
QInputDialog, QFontDialog, QColorDialog, QFileDialog
'''
绘图API: 绘制各种图形 的案例
弧形
圆形
椭圆
矩形(正方形)
多边形
'''
class drawDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 400, 520)
# 设置窗口标题
self.setWindowTitle('在窗口上绘制不同的各种图形 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
def paintEvent(self,event):
qp = QPainter(self)
qp.begin(self)
print("- - -")
qp.setPen(Qt.blue)
# 画弧线
rect = QRect(0,10,100,100)
# alen : 1个alen 等于 1/16度 45*16
qp.drawArc(rect,0,50*16)
#通过弧绘制圆形
qp.setPen(Qt.red)
qp.drawArc(120,10,100,100,0,360*16)
# 绘制带弦的弧
qp.drawChord(10,120,100,100,12,130*16)
# 绘制扇形
qp.drawPie(10,240,100,100,12,130*16)
# 绘制椭圆
qp.drawEllipse(120,120,150,100)
# 绘制五边形
point1 = QPoint(140,380)
point2 = QPoint(270,380)
point3 = QPoint(290,510)
point4 = QPoint(290,580)
point5 = QPoint(200,530)
polygon = QPolygon([point1,point2,point3,point4,point5])
qp.drawPolygon(polygon)
# 绘制图像
image = QImage('../web.ico')
rect = QRect(10,400,image.width()*3,image.height()*3)
qp.drawImage(rect,image)
qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = drawDemo()
w.show()
sys.exit(app.exec_())