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_())'''''
I Shall be greatly thankful...
A label is generally used to identify a nearby text box or other widget. Some labels can respond to events such as mouse clicks, allowing the text of the label to be copied, but this is not standard user-interface practice. To use Labels in PyQt5 we have to import QLabel from PyQt5.QtWidgets , below is the syntax to do so.
Qt automatically grabs the mouse when a mouse button is pressed inside a widget; the widget will continue to receive mouse events until the last mouse button is released. A mouse event contains a special accept flag that indicates whether the receiver wants the event. You should call ignore () if the mouse event is not handled by your widget.
The QMouseEvent class contains parameters that describe a mouse event. More … Mouse events occur when a mouse button is pressed or released inside a widget, or when the mouse cursor is moved. Mouse move events will occur only when a mouse button is pressed down, unless mouse tracking has been enabled with setMouseTracking () .
While designing any GUI (Graphical User Interface) there is a need to display plain text on screen, in order to do so Label widget is used in PyQt5. A label is a graphical control element which displays text on a form. It is usually a static control; having no interactivity.
If you've already imported
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
there is no need to
from PyQt5 import *
Once you've imported QtCore, you no longer need to call its functions/classes with 'QtCore.QEvent', Just using QEvent is fine
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_())
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
Please find the reference:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Label(QLabel):
def __init__(self, *args, **kwargs):
QLabel.__init__(self, *args, **kwargs)
def enterEvent(self, event):
print("hovered")
def leaveEvent(self, event):
print("left")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With