鼠标跟随

from PyQt5.Qt import *
import sys


class Window(QWidget):
    def __init__(self):
        super().__init__()
        # 设置控件
        self.setWindowTitle('鼠标的相关操作')
        # 设置大小
        self.resize(500, 500)
        self.move(200, 200)
        self.setMouseTracking(True)

        # 更换鼠标的样式
        pixmap = QPixmap('xxx.png').scaled(50, 50)
        cursor = QCursor(pixmap)
        self.setCursor(cursor)

        lable = QLabel(self)
        # # 标签
        # self.label = lable
        lable.setText('blblblblblbl')
        lable.move(100, 100)
        lable.setStyleSheet("background-color:cyan;")

    def mouseMoveEvent(self, mv):
        print('鼠标移动', mv.localPos())
        lable = self.findChild(QLabel)  # 找到运动的鼠标位置
        lable.move(mv.localPos().x(), mv.localPos().y())


app = QApplication(sys.argv)
# 创建控件
window = Window()
# 展示控件
window.show()
# 退出
sys.exit(app.exec_())

你可能感兴趣的:(鼠标跟随)