赞
踩
摘自《PyQt5快速开发实战》第八章——PyQt5图形与特效
- from PyQt5.QtWidgets import QWidget, QApplication
- from PyQt5.QtGui import QPainter, QPainterPath, QPixmap
- from PyQt5.QtCore import Qt, QPoint
- import sys
-
-
- class Example(QWidget):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.setWindowTitle('画线')
- self.pix = QPixmap()
- self.lastPoint = QPoint()
- self.endPoint = QPoint()
- self.initUI()
-
- def initUI(self):
- self.resize(600, 500)
- self.pix = QPixmap(600, 500)
- self.pix.fill(Qt.white)
-
- def paintEvent(self, event): #重写paintEvent事件
- pp = QPainter(self.pix)
- pp.drawLine(self.lastPoint, self.endPoint)
- self.lastPoint = self.endPoint
- painter = QPainter(self)
- painter.drawPixmap(0,0,self.pix)
-
- def mousePressEvent(self, event): #重写鼠标按下事件
- if event.button() == Qt.LeftButton:
- self.lastPoint = event.pos()
- self.endPoint = self.lastPoint
-
- def mouseMoveEvent(self, event): #重写鼠标移动事件
- if event.buttons() and Qt.LeftButton:
- self.endPoint = event.pos()
- self.update() #更新绘图事件,每次执行update都会触发一次paintEvent(self, event)函数
-
- def mouseReleaseEvent(self, event): #重写鼠标释放事件
- if event.button() == Qt.LeftButton:
- self.endPoint = event.pos()
- self.update()
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ex = Example()
- ex.show()
- sys.exit(app.exec_())

运行结果:
- from PyQt5.QtWidgets import QWidget, QApplication
- from PyQt5.QtGui import QPainter, QPixmap, QPen
- from PyQt5.QtCore import Qt, QRect
- import sys
-
- class Example(QWidget):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.setWindowTitle('绘制矩形')
- self.setCursor(Qt.CrossCursor) #设置十字光标
- self.x0 = 0
- self.y0 = 0
- self.x1 = 0
- self.y1 = 0
- self.initUI()
-
- def initUI(self):
- self.resize(600, 500)
- self.pix = QPixmap(600, 500) #创建一个QPixmap画板
- self.pix.fill(Qt.white)
-
- def paintEvent(self, event):
- rect = QRect(self.x0, self.y0, abs(self.x1 - self.x0), abs(self.y1 - self.y0))
- painter = QPainter(self)
- painter.setPen(QPen(Qt.red, 2, Qt.SolidLine))
- painter.drawRect(rect)
-
- def mousePressEvent(self, event): #重写鼠标按下事件
- if event.button() == Qt.LeftButton:
- self.flag = True
- self.x0 = event.x()
- self.y0 = event.y()
-
- def mouseMoveEvent(self, event): #重写鼠标移动事件
- if event.buttons() and Qt.LeftButton:
- if self.flag:
- self.x1 = event.x()
- self.y1 = event.y()
- self.update() #更新绘图事件,每次执行update都会触发一次paintEvent(self, event)函数
-
- def mouseReleaseEvent(self, event): #重写鼠标释放事件
- if event.button() == Qt.LeftButton:
- self.flag = False
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ex = Example()
- ex.show()
- sys.exit(app.exec_())

运行结果:
- from PyQt5.QtWidgets import QWidget, QApplication
- from PyQt5.QtGui import QPainter, QPixmap, QPen
- from PyQt5.QtCore import Qt, QPoint
- import sys
-
-
- class Example(QWidget):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.setWindowTitle('双缓冲绘图')
- self.setCursor(Qt.CrossCursor) # 设置十字光标
- self.pix = QPixmap()
- self.lastPoint = QPoint()
- self.endPoint = QPoint()
- self.tmpPix = QPixmap()
- self.isDrawing = False
- self.initUI()
-
- def initUI(self):
- self.resize(600, 500)
- self.pix = QPixmap(600, 500) # 创建一个QPixmap画板
- self.pix.fill(Qt.white)
-
- def paintEvent(self, event):
- painter = QPainter(self)
- x = self.lastPoint.x()
- y = self.lastPoint.y()
- w = self.endPoint.x() - x
- h = self.endPoint.y() - y
- if self.isDrawing:
- self.tmpPix = self.pix
- pp = QPainter(self.tmpPix)
- pp.drawRect(x,y,w,h)
- painter.drawPixmap(0, 0, self.tmpPix)
- else:
- pp = QPainter(self.pix)
- pp.drawRect(x,y,w,h)
- painter.drawPixmap(0, 0, self.pix)
- def mousePressEvent(self, event): # 重写鼠标按下事件
- if event.button() == Qt.LeftButton:
- self.lastPoint = event.pos()
- self.endPoint = self.lastPoint
- self.isDrawing = True
-
- def mouseReleaseEvent(self, event): # 重写鼠标释放事件
- if event.button() == Qt.LeftButton:
- self.endPoint = event.pos()
- self.update()
- self.isDrawing = False
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ex = Example()
- ex.show()
- sys.exit(app.exec_())

运行结果:
- import sys, random
- from PyQt5.QtWidgets import QWidget, QApplication
- from PyQt5.QtGui import QPainter, QColor, QPen
- from PyQt5.QtCore import Qt
-
-
- class Example(QWidget):
- def __init__(self):
- super().__init__()
- self.initUI()
- self.isdraw = False
-
- def initUI(self):
- self.setGeometry(300, 300, 280, 170)
- self.setWindowTitle("Points")
- self.show()
-
- def paintEvent(self, e):
- qp =QPainter()
- qp.begin(self)
- self.drawPoints(qp)
- qp.end()
-
- def drawPoints(self, qp):
- pen = QPen(Qt.black, 5) #创建画笔类并设置画笔属性,第二个参数设置画笔大小
- qp.setPen(pen)
- if self.isdraw:
- qp.drawPoint(self.point)
-
- def mousePressEvent(self, event): # 重写鼠标按下事件
- if event.button() == Qt.LeftButton:
- self.isdraw = True
- self.point = event.pos()
-
- def mouseReleaseEvent(self, event): # 重写鼠标释放事件
- if event.button() == Qt.LeftButton:
- self.update()
-
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- ex = Example()
- sys.exit(app.exec_())

运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。