老师原课件下载地址:
有积分的朋友,支持下我,打赏也OK。
不下载也可以,我每节课会吧代码贴出来
视频链接
课程重点
代码位置
完整代码
'''
绘制各种图形
弧
圆形
椭圆
矩形(正方形)
多边形
绘制图像
'''
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.resize(300,600)
self.setWindowTitle('绘制各种图形')
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
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)
# 绘制5边形
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])
qp.drawPolygon(polygon)
# 绘制图像
image = QImage('./images/book1.png')
rect = QRect(10, 400, image.width()/3, image.height()/3)
qp.drawImage(rect,image)
qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = DrawAll()
main.show()
sys.exit(app.exec_())
遇到的问题
改写的时候,因为paintEevnt()是专用方法,所以必须调用QWidget才能正常使用
因为我是用self.ui调用的窗口,所以这里和默认的paintEevnt()方法不配套,所以最后不显示内容,好烦。。解决不了
哈哈,终于解决了,找到了一片文章:
>>>PyQt5学习笔记4_loadUi加载ui文件
改写效果展示
改写注意点
改写代码(QTdesigner模式)
# -*- coding:utf-8 -*-
'''
@Author: knocky
@Blog: https://blog.csdn.net/zzx188891020
@E-mail: 188891020@qq.com
@File: class52.py
@CreateTime: 2020/5/16 19:47
'''
import sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5 import uic
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class my_form(QWidget):
def __init__(self):
super().__init__()
# self.ui = uic.loadUi("../ui_package/class52.ui")
uic.loadUi('../ui_package/class52.ui', self)
def paintEvent(self, QPaintEvent):
print('1')
qp = QPainter()
qp.begin(self)
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)
# 绘制5边形
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])
qp.drawPolygon(polygon)
# 绘制图像
image = QImage('../2.images/book1.png')
rect = QRect(10, 400, int(image.width()/3), int(image.height()/3))
qp.drawImage(rect,image)
qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = my_form()
main.show()
sys.exit(app.exec_())