当前位置:   article > 正文

PyQt5 事件处理之绘图_pyqt5实时画图

pyqt5实时画图

摘自《PyQt5快速开发实战》第八章——PyQt5图形与特效 

  • 画线

  1. from PyQt5.QtWidgets import QWidget, QApplication
  2. from PyQt5.QtGui import QPainter, QPainterPath, QPixmap
  3. from PyQt5.QtCore import Qt, QPoint
  4. import sys
  5. class Example(QWidget):
  6. def __init__(self, parent=None):
  7. super().__init__(parent)
  8. self.setWindowTitle('画线')
  9. self.pix = QPixmap()
  10. self.lastPoint = QPoint()
  11. self.endPoint = QPoint()
  12. self.initUI()
  13. def initUI(self):
  14. self.resize(600, 500)
  15. self.pix = QPixmap(600, 500)
  16. self.pix.fill(Qt.white)
  17. def paintEvent(self, event): #重写paintEvent事件
  18. pp = QPainter(self.pix)
  19. pp.drawLine(self.lastPoint, self.endPoint)
  20. self.lastPoint = self.endPoint
  21. painter = QPainter(self)
  22. painter.drawPixmap(0,0,self.pix)
  23. def mousePressEvent(self, event): #重写鼠标按下事件
  24. if event.button() == Qt.LeftButton:
  25. self.lastPoint = event.pos()
  26. self.endPoint = self.lastPoint
  27. def mouseMoveEvent(self, event): #重写鼠标移动事件
  28. if event.buttons() and Qt.LeftButton:
  29. self.endPoint = event.pos()
  30. self.update() #更新绘图事件,每次执行update都会触发一次paintEvent(self, event)函数
  31. def mouseReleaseEvent(self, event): #重写鼠标释放事件
  32. if event.button() == Qt.LeftButton:
  33. self.endPoint = event.pos()
  34. self.update()
  35. if __name__ == '__main__':
  36. app = QApplication(sys.argv)
  37. ex = Example()
  38. ex.show()
  39. sys.exit(app.exec_())

运行结果:

 

  •  画矩形

  1. from PyQt5.QtWidgets import QWidget, QApplication
  2. from PyQt5.QtGui import QPainter, QPixmap, QPen
  3. from PyQt5.QtCore import Qt, QRect
  4. import sys
  5. class Example(QWidget):
  6. def __init__(self, parent=None):
  7. super().__init__(parent)
  8. self.setWindowTitle('绘制矩形')
  9. self.setCursor(Qt.CrossCursor) #设置十字光标
  10. self.x0 = 0
  11. self.y0 = 0
  12. self.x1 = 0
  13. self.y1 = 0
  14. self.initUI()
  15. def initUI(self):
  16. self.resize(600, 500)
  17. self.pix = QPixmap(600, 500) #创建一个QPixmap画板
  18. self.pix.fill(Qt.white)
  19. def paintEvent(self, event):
  20. rect = QRect(self.x0, self.y0, abs(self.x1 - self.x0), abs(self.y1 - self.y0))
  21. painter = QPainter(self)
  22. painter.setPen(QPen(Qt.red, 2, Qt.SolidLine))
  23. painter.drawRect(rect)
  24. def mousePressEvent(self, event): #重写鼠标按下事件
  25. if event.button() == Qt.LeftButton:
  26. self.flag = True
  27. self.x0 = event.x()
  28. self.y0 = event.y()
  29. def mouseMoveEvent(self, event): #重写鼠标移动事件
  30. if event.buttons() and Qt.LeftButton:
  31. if self.flag:
  32. self.x1 = event.x()
  33. self.y1 = event.y()
  34. self.update() #更新绘图事件,每次执行update都会触发一次paintEvent(self, event)函数
  35. def mouseReleaseEvent(self, event): #重写鼠标释放事件
  36. if event.button() == Qt.LeftButton:
  37. self.flag = False
  38. if __name__ == '__main__':
  39. app = QApplication(sys.argv)
  40. ex = Example()
  41. ex.show()
  42. sys.exit(app.exec_())

运行结果:

 

  • 多个矩形

  1. from PyQt5.QtWidgets import QWidget, QApplication
  2. from PyQt5.QtGui import QPainter, QPixmap, QPen
  3. from PyQt5.QtCore import Qt, QPoint
  4. import sys
  5. class Example(QWidget):
  6. def __init__(self, parent=None):
  7. super().__init__(parent)
  8. self.setWindowTitle('双缓冲绘图')
  9. self.setCursor(Qt.CrossCursor) # 设置十字光标
  10. self.pix = QPixmap()
  11. self.lastPoint = QPoint()
  12. self.endPoint = QPoint()
  13. self.tmpPix = QPixmap()
  14. self.isDrawing = False
  15. self.initUI()
  16. def initUI(self):
  17. self.resize(600, 500)
  18. self.pix = QPixmap(600, 500) # 创建一个QPixmap画板
  19. self.pix.fill(Qt.white)
  20. def paintEvent(self, event):
  21. painter = QPainter(self)
  22. x = self.lastPoint.x()
  23. y = self.lastPoint.y()
  24. w = self.endPoint.x() - x
  25. h = self.endPoint.y() - y
  26. if self.isDrawing:
  27. self.tmpPix = self.pix
  28. pp = QPainter(self.tmpPix)
  29. pp.drawRect(x,y,w,h)
  30. painter.drawPixmap(0, 0, self.tmpPix)
  31. else:
  32. pp = QPainter(self.pix)
  33. pp.drawRect(x,y,w,h)
  34. painter.drawPixmap(0, 0, self.pix)
  35. def mousePressEvent(self, event): # 重写鼠标按下事件
  36. if event.button() == Qt.LeftButton:
  37. self.lastPoint = event.pos()
  38. self.endPoint = self.lastPoint
  39. self.isDrawing = True
  40. def mouseReleaseEvent(self, event): # 重写鼠标释放事件
  41. if event.button() == Qt.LeftButton:
  42. self.endPoint = event.pos()
  43. self.update()
  44. self.isDrawing = False
  45. if __name__ == '__main__':
  46. app = QApplication(sys.argv)
  47. ex = Example()
  48. ex.show()
  49. sys.exit(app.exec_())

运行结果:

 

  • 画点

  1. import sys, random
  2. from PyQt5.QtWidgets import QWidget, QApplication
  3. from PyQt5.QtGui import QPainter, QColor, QPen
  4. from PyQt5.QtCore import Qt
  5. class Example(QWidget):
  6. def __init__(self):
  7. super().__init__()
  8. self.initUI()
  9. self.isdraw = False
  10. def initUI(self):
  11. self.setGeometry(300, 300, 280, 170)
  12. self.setWindowTitle("Points")
  13. self.show()
  14. def paintEvent(self, e):
  15. qp =QPainter()
  16. qp.begin(self)
  17. self.drawPoints(qp)
  18. qp.end()
  19. def drawPoints(self, qp):
  20. pen = QPen(Qt.black, 5) #创建画笔类并设置画笔属性,第二个参数设置画笔大小
  21. qp.setPen(pen)
  22. if self.isdraw:
  23. qp.drawPoint(self.point)
  24. def mousePressEvent(self, event): # 重写鼠标按下事件
  25. if event.button() == Qt.LeftButton:
  26. self.isdraw = True
  27. self.point = event.pos()
  28. def mouseReleaseEvent(self, event): # 重写鼠标释放事件
  29. if event.button() == Qt.LeftButton:
  30. self.update()
  31. if __name__ == "__main__":
  32. app = QApplication(sys.argv)
  33. ex = Example()
  34. sys.exit(app.exec_())

 运行结果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/897421
推荐阅读
相关标签
  

闽ICP备14008679号