PyQT5 标签的鼠标悬停事件 [英] Mouseover event for a PyQT5 Label

查看:731
本文介绍了PyQT5 标签的鼠标悬停事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 python 的新手,我希望如果我将鼠标移到带有文本停止的标签上,那么它应该将变量 Stop 的值更改为 True,以便我可以暂停/停止我的程序.我看过代码PyQT 标签的鼠标悬停事件过滤器并尝试运行它但没有显示任何内容请指导我...代码如下

I am a newbie to python i want that if i move my mouse over the label with text stop on it then it should change the value of a variable Stop to True so that i may pause/Stop ,my program. i have looked the code at Mouseover event filter for a PyQT Label and tried to run it but nothing is being shown up Please guide me... the code is as under

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import *
import sys
class mouseoverEvent(QtCore.QObject):
    def __init__(self, parent):
        super(mouseoverEvent, self).__init__(parent)
        self.initUI()
    def eventFilter(self, object, event):
        if event.type() == QtCore.QEvent.MouseMove:
            print( "mousemove!")
            return True
        else:
            return False

    def initUI(self):
        self.filter = mouseoverEvent(self)
        self.label.installEventFilter(self.filter)
        self.lbl=QLabel(self)
        self.lbl.setText(self,"hellojjj")
        self.setGeometry(1000, 30, 300, 100)
        self.setWindowTitle('QLineEdit')

        self.show()

'''if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = mouseoverEvent()
    sys.exit(app.exec_())'''''

我将不胜感激...

推荐答案

  1. 如果您已经导入

  1. If you've already imported

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

没有必要

from PyQt5 import *

  • 导入 QtCore 后,您不再需要使用QtCore.QEvent"调用其函数/类,只需使用 QEvent 即可

  • Once you've imported QtCore, you no longer need to call its functions/classes with 'QtCore.QEvent', Just using QEvent is fine

    我相信您链接的问题使用了 PyQt4.在 PyQt5 中,类的初始化过程发生了变化

    I believe the question you linked to used PyQt4. In PyQt5, the initialization procedure for the class changed

    下面的代码应该可以工作.

    The code below should work.

    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    import sys
    
    class mouseoverEvent(QWidget):
    
        def __init__(self):
            super().__init__()
            self.stop = False # your 'stop' variable
            self.initUI()
    
        def initUI(self):
            self.lbl=QLabel(self)
            self.lbl.setText("Hover over me to stop the program")
            self.lbl.installEventFilter(self)
            self.setGeometry(1000, 30, 300, 100)
            self.setWindowTitle('QLineEdit')
    
            self.show()
    
        def eventFilter(self, object, event):
            if event.type() == QEvent.Enter:
                print("Mouse is over the label")
                self.stop = True
                print('program stop is', self.stop)
                return True
            elif event.type() == QEvent.Leave:
                print("Mouse is not over the label")
                self.stop = False
                print('program stop is', self.stop)
            return False
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = mouseoverEvent()
        sys.exit(app.exec_())
    

    如果您只想在带有特定文本的标签上激活停止,请将 eventFilter 函数更改为:

    if you only want the stop to activate over a label with certain text change your eventFilter function to:

    def eventFilter(self, object, event):
        if hasattr(object, 'text'): #check to see if the object has text, otherwise if you hover over something without text, PyQt will return an error
            if object.text() == "Hover over me to stop the program":
                if event.type() == QEvent.Enter:
                    print("Mouse is over the label")
                    self.stop = True
                    print('program stop is', self.stop)
                    return True
                elif event.type() == QEvent.Leave:
                    print("Mouse is not over the label")
                    self.stop = False
                    print('program stop is', self.stop)
        return False
    

    这篇关于PyQT5 标签的鼠标悬停事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆