Here is an example from the training material (abridged):
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow, QTextEdit
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.label = QLabel("Click in this window")
self.setCentralWidget(self.label)
def mousePressEvent(self, e):
print (f"mousePressEvent:: {e.buttons()}")
if e.button() == Qt.LeftButton:
# здесь обрабатываем нажатие левой кнопки
self.label.setText("mousePressEvent LEFT")
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
When you click on the mouse button, an error appears:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [1], in MainWindow.mousePressEvent(self, e)
12 def mousePressEvent(self, e):
13 print (f"mousePressEvent:: {e.buttons()}")
---> 15 if e.button() == Qt.LeftButton:
16 # здесь обрабатываем нажатие левой кнопки
17 self.label.setText("mousePressEvent LEFT")
AttributeError: type object 'Qt' has no attribute 'LeftButton'
Help! There are some answers on this topic on the Internet, but none came up to me. What am I doing wrong?
Thanks! Sincerely, Alexander
There are some answers on this topic on the Internet, but none came up to me.
This solution doesn't work either: How to check MouseButtonPress event in PyQt6?
AttributeError Traceback (most recent call last)
Input In [3], in <module>
19 app = QApplication(sys.argv)
---> 21 print(Qt.white)
23 window = MainWindow()
24 window.show()
AttributeError: type object 'Qt' has no attribute 'white'