python之pyQt5实例:鼠标获取label控件图片上的点

1、鼠标点击就可以,不区分左键、中键和右键

from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
​
class MyLabel(QLabel):
    def mousePressEvent(self, event):
        if self.pixmap() is not None:
            x = event.x()
            y = event.y()
            print("鼠标点击的坐标:", x, y)
​
app = QApplication([])
​
label = MyLabel()
​
# 通过QPixmap设置图片
pixmap = QPixmap(r'xxx.png')
label.setPixmap(pixmap)
​
# 设置允许鼠标点击事件传递到下层控件
label.setAttribute(Qt.WA_TransparentForMouseEvents)
label.show()
​
app.exec_()
​

2、只要鼠标左键

from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
​
class MyLabel(QLabel):
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton and self.pixmap() is not None:
            x = event.x()
            y = event.y()
            print("鼠标左键点击的坐标:", x, y)
​
app = QApplication([])
​
label = MyLabel()
​
# 通过QPixmap设置图片
pixmap = QPixmap(r'xxx.png')
label.setPixmap(pixmap)
​
# 设置允许鼠标点击事件传递到下层控件
label.setAttribute(Qt.WA_TransparentForMouseEvents)
label.show()
​
app.exec_()

3、鼠标左键+把点标记出来

from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QPixmap, QPainter, QPen
from PyQt5.QtCore import Qt,QPoint
​
​
class MyLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.point = None
​
    def setPoint(self, point):
        self.point = point
        self.update()
​
    def paintEvent(self, event):
        super().paintEvent(event)
        if self.point:
            painter = QPainter(self)
            pen = QPen(Qt.red, 10)
            painter.setPen(pen)
            painter.drawPoint(self.point)
​
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton and self.pixmap() is not None:
            x = event.x()
            y = event.y()
            self.setPoint(QPoint(x, y))
            print("鼠标左键点击的坐标:", x, y)
​
​
app = QApplication([])
​
label = MyLabel()
​
# 通过QPixmap设置图片
pixmap = QPixmap(r'xxx.png')
label.setPixmap(pixmap)
​
# 设置允许鼠标点击事件传递到下层控件
label.setAttribute(Qt.WA_TransparentForMouseEvents)
label.show()
​
app.exec_()

python之pyQt5实例:鼠标获取label控件图片上的点_第1张图片

 4、鼠标左键+把点标记出来+点击下一个点时,前一个点的标记要保留

from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QPixmap, QPainter, QPen
from PyQt5.QtCore import Qt, QPoint
​
class MyLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.points = []
​
    def setPoints(self, points):
        self.points = points
        self.update()
​
    def paintEvent(self, event):
        super().paintEvent(event)
        if self.pixmap() is not None:
            painter = QPainter(self)
            pen = QPen(Qt.red, 10)
            painter.setPen(pen)
            for point in self.points:
                painter.drawPoint(point)
​
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton and self.pixmap() is not None:
            x = event.x()
            y = event.y()
            point = QPoint(x, y)
            self.points.append(point)
            self.update()
            print("鼠标左键点击的坐标:", x, y)
​
app = QApplication([])
​
label = MyLabel()
​
# 通过QPixmap设置图片
pixmap = QPixmap(r'xxx.png')
label.setPixmap(pixmap)
​
# 设置允许鼠标点击事件传递到下层控件
label.setAttribute(Qt.WA_TransparentForMouseEvents)
label.show()
​
app.exec_()

python之pyQt5实例:鼠标获取label控件图片上的点_第2张图片

5、鼠标左键+把点标记出来+点击下一个点时,前一个点的标记要保留+文件对话框选择图片

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QFileDialog, QWidget
from PyQt5.QtGui import QPixmap, QPainter, QPen
from PyQt5.QtCore import Qt, QPoint


class MyLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.points = []

    def setPoints(self, points):
        self.points = points
        self.update()

    def paintEvent(self, event):
        super().paintEvent(event)
        if self.pixmap() is not None:
            painter = QPainter(self)
            pen = QPen(Qt.red, 4)
            painter.setPen(pen)
            for point in self.points:
                painter.drawPoint(point)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton and self.pixmap() is not None:
            x = event.x()
            y = event.y()
            point = QPoint(x, y)
            self.points.append(point)
            self.update()
            print("鼠标左键点击的坐标:", x, y)


def openImage():
    file_dialog = QFileDialog()
    image_path, _ = file_dialog.getOpenFileName(None, "打开图像", "", "图像文件 (*.jpg *.png)")

    if image_path:
        pixmap = QPixmap(image_path)
        label.setPixmap(pixmap.scaled(label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window_width = 800
    window_height = 600

    window = QWidget()
    window.setWindowTitle("图像查看器")
    window.resize(window_width, window_height)

    label = MyLabel(window)
    label.setGeometry(50, 50, window_width - 100, window_height - 200)
    label.setAlignment(Qt.AlignCenter)

    button = QPushButton("选择图片", window)
    button.setGeometry(50, window_height - 125, 100, 30)
    button.clicked.connect(openImage)

    window.show()

    sys.exit(app.exec_())

python之pyQt5实例:鼠标获取label控件图片上的点_第3张图片

你可能感兴趣的:(python,qt,计算机外设)