pyqt5鼠标在图片中绘画代码怎么写
时间: 2024-05-07 09:16:37 浏览: 106
你好,关于你的问题,以下是一段Python代码,可以实现在PyQt5中用鼠标在图片中绘画:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtGui import QPixmap, QPainter, QPen
from PyQt5.QtCore import Qt, QPoint
class MyLabel(QLabel):
def __init__(self, parent=None):
super(MyLabel, self).__init__(parent)
self.setPixmap(QPixmap("your_image_path")) # 请将 your_image_path 替换为你的图片路径
self.pos_xy = []
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.pos_xy.append((event.pos().x(), event.pos().y()))
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.pos_xy = []
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.pixmap())
painter.setPen(QPen(Qt.red, 5, Qt.SolidLine))
for pos in self.pos_xy:
painter.drawPoint(QPoint(*pos))
if __name__ == '__main__':
app = QApplication([])
window = QMainWindow()
label = MyLabel(window)
window.setCentralWidget(label)
window.show()
app.exec_()
```
希望能帮到你!
阅读全文