1

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)
  • 3
    Unfortunately, enums are all scoped in PyQt6, so you're now forced to write ridiculously verbose code like this: `Qt.MouseButton.LeftButton`. – ekhumoro Dec 14 '21 at 16:55
  • Thank you! It did @ekhumoro Where can you find that? I tried to search in the documentation but didn't saw that – pdaranda661 Dec 14 '21 at 17:14
  • Yes, it should really be noted in the PyQt6 docs, because it is a breaking change and unscoped enums were never deprecated in PyQt5. There is something about it in the PyQt5 docs: [Enums](https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#enums). – ekhumoro Dec 14 '21 at 17:27

0 Answers0