I was following this tutorial to learn the basics about Qt and start developing some GUI applications, but when I got to the part of determining which mouse event occurred, it seems that python is unable to find the attributes related to the mouse events.
To put some context, I include the following packages:
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QLineEdit, QVBoxLayout, QWidget, QTextEdit
from PyQt6.QtCore import Qt
After that, I create this simple Window class as in the tutorial:
class MainWindow5(QMainWindow):
def __init__(self):
super().__init__()
self.label = QLabel("Click in this window")
self.setCentralWidget(self.label)
self.setMouseTracking(True)
def mouseMoveEvent(self, e):
self.label.setText("mouseMoveEvent")
def mousePressEvent(self, e):
self.label.setText("mousePressEvent")
if e.button() == Qt.LeftButton:
self.label.setText("mousePressEvent Left")
elif e.button() == Qt.MiddleButton:
self.label.setText("mousePressEvent Middle")
elif e.button() == Qt.RightButton:
self.label.setText("mousePressEvent Right")
def mouseReleaseEvent(self, e):
self.label.setText("mouseReleaseEvent")
if e.button() == Qt.LeftButton:
self.label.setText("mouseReleaseEvent Left")
elif e.button() == Qt.MiddleButton:
self.label.setText("mouseReleaseEvent Middle")
elif e.button() == Qt.RightButton:
self.label.setText("mouseReleaseEvent Right")
def mouseDoubleClickEvent(self, e):
self.label.setText("mouseDoubleClickEvent")
if e.button() == Qt.LeftButton:
self.label.setText("mouseDoubleClickEvent Left")
elif e.button() == Qt.MiddleButton:
self.label.setText("mouseDoubleClickEvent Middle")
elif e.button() == Qt.RightButton:
self.label.setText("mouseDoubleClickEvent Right")
Once I try to execute it and I click in the window, I obtain the following error:
Traceback (most recent call last):
File "signals.py", line 127, in mousePressEvent
if e.button() == Qt.LeftButton:
AttributeError: module 'PyQt6.QtCore' has no attribute 'LeftButton'
Aborted (core dumped)