python之pyqt专栏9-鼠标事件

目录

需求

UI界面

代码实现

代码解析:

Label初始化设置

重写鼠标按下事件

重写鼠标释放事件

重写鼠标移动事件

运行结果


需求

当鼠标进入窗口时,点击鼠标左键,出现一个label并在显示光标在窗口的坐标;按住左键不释放拖动鼠标,label的坐标信息跟着光标位置变化,当左键被释放时,label消失不见。

UI界面

创建一个QWidget窗口

代码实现

python 复制代码
# 导入sys模块
import sys

from PyQt6.QtCore import Qt
# PyQt6.QtWidgets模块中导入QApplication, QWidget
from PyQt6.QtWidgets import QApplication, QWidget,QLabel

# untitled模块中导入Ui_Form类
from untitled import Ui_Form


class MyMainForm(QWidget, Ui_Form):
    def __init__(self, parent=None):
        # 调用父类的构造函数
        super(MyMainForm, self).__init__(parent)
        # 调用继承Ui_Form过来的setupUi函数
        self.setupUi(self)

        self.label = QLabel(self)
        self.label.setText("x = 0, y = 0            ")
        self.label.hide()



    def mouseMoveEvent(self, a0):
        self.label.move(a0.pos())
        self.label.setText(f"x = {a0.pos().x()} , y = {a0.pos().y()}")

    def mousePressEvent(self, a0):
        if(a0.button() == Qt.MouseButton.LeftButton):
            self.label.show()
            self.label.move(a0.pos())
            self.label.setText(f"x = {a0.pos().x()} , y = {a0.pos().y()}")


    def mouseReleaseEvent(self, a0):
        if (a0.button() == Qt.MouseButton.LeftButton):
            self.label.hide()



# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # 实例化应用
    app = QApplication(sys.argv)
    # 实例化MyMainForm
    myw = MyMainForm()
    myw.show()
    # 启动应用程序的事件循环并等待用户交互,直到应用程序关闭。
    sys.exit(app.exec())

如果要在自己的部件上实现鼠标事件,可以根据需要在部件类对下面几种方法进行重写:

mousePressEvent(),鼠标按下事件

mouseReleaseEvent(),鼠标释放事件

mouseDoubleClickEvent(),鼠标双击事件

mouseMoveEvent(),鼠标移动事件

在个需求中,需要用到**mousePressEvent(),mouseReleaseEvent(),mouseMoveEvent()事件,**因此对他们进行重写。

此外,可以在QWidget的方法中找到它们

代码解析:

Label初始化设置
python 复制代码
self.label = QLabel(self)
self.label.setText("x = 0, y = 0            ")
self.label.hide()

1:在类的构造函数,添加一个label标签,并设置它的父对象是当前窗口

python 复制代码
self.label = QLabel(self)

2:设置Label文本内容,"y =0 "后面有很多空格,目的是为了占位,不然当坐标值变大会显示不全

python 复制代码
self.label.setText("x = 0, y = 0            ")

3.隐藏标签,根据需求点击的时候才能出现。

python 复制代码
self.label.hide()
重写鼠标按下事件
python 复制代码
    def mousePressEvent(self, a0):
        if(a0.button() == Qt.MouseButton.LeftButton):
            self.label.show()
            self.label.move(a0.pos())
            self.label.setText(f"x = {a0.pos().x()} , y = {a0.pos().y()}")

1.a0是事件对象,通过button()可以获取被按下的按键,而Qt.MouseButton是枚举,里面定义了鼠标按键编号

python 复制代码
if(a0.button() == Qt.MouseButton.LeftButton):

注:如果是鼠标多个按键同时被按下,可以调用通过buttons(),button只是返回一个按键

2.a0事件对象,通过调用pos获取,相对于当前窗口的位置。移动label到当前鼠标的位置

python 复制代码
 self.label.move(a0.pos())

3.将坐标信息设置为label的文本信息

python 复制代码
self.label.setText(f"x = {a0.pos().x()} , y = {a0.pos().y()}")
重写鼠标释放事件

当鼠标左键被释放时,隐藏label

python 复制代码
    def mouseReleaseEvent(self, a0):
        if (a0.button() == Qt.MouseButton.LeftButton):
            self.label.hide()
重写鼠标移动事件

获取鼠标位置,移动label到当前鼠标的位置;坐标信息设置为label的文本信息

python 复制代码
    def mouseMoveEvent(self, a0):
        self.label.move(a0.pos())
        self.label.setText(f"x = {a0.pos().x()} , y = {a0.pos().y()}")

运行结果

鼠标事件

相关推荐
sword devil9001 天前
PYQT实战:智能家居中控
python·智能家居·pyqt
OICQQ676580086 天前
创建一个基于YOLOv8+PyQt界面的驾驶员疲劳驾驶检测系统 实现对驾驶员疲劳状态的打哈欠检测,头部下垂 疲劳眼睛检测识别
yolo·pyqt·疲劳驾驶·检测识别·驾驶员检测·打哈欠检测·眼睛疲劳
小灰灰搞电子15 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
越甲八千16 天前
pyqt 简单条码系统
数据库·microsoft·pyqt
重生之我在厦门做测试17 天前
基于pyqt开发串口和对应映射值(path)的显示工具
pyqt
hvinsion20 天前
【开源解析】基于Python+Qt打造智能应用时长统计工具 - 你的数字生活分析师
python·pyqt·开源软件·应用时长统计
毕设做完了吗?23 天前
基于YOLOv8+Deepface的人脸检测与识别系统
python·yolo·毕业设计·pyqt
懷淰メ1 个月前
python3GUI--基于PyQt5+DeepSort+YOLOv8智能人员入侵检测系统(详细图文介绍)
开发语言·深度学习·yolo·目标检测·pyqt·课程设计·deepsort
Humbunklung1 个月前
PySide6 GUI 学习笔记——常用类及控件使用方法(多行文本控件QTextEdit)
笔记·python·学习·pyqt
En^_^Joy1 个月前
PyQt常用控件的使用:QFileDialog、QMessageBox、QTreeWidget、QRadioButton等
开发语言·python·pyqt