目录
一、实战场景
实战场景:Python 中如何创建 PyQt6 的事件对象
二、主要知识点
三、菜鸟实战
事件对象是一个 Python object,包含了一系列描述这个事件的属性,具体内容要看触发的事件。
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QWidget, QApplication, QGridLayout, QLabel
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
x = 0
y = 0
self.text = f'x: {x}, y: {y}'
self.label = QLabel(self.text, self)
grid.addWidget(self.label, 0, 0, Qt.Alignment.AlignTop)
self.setMouseTracking(True)
self.setLayout(grid)
self.setGeometry(300, 300, 450, 300)
self.setWindowTitle('Event object')
self.show()
def mouseMoveEvent(self, e):
x = int(e.position().x())
y = int(e.position().y())
text = f'x: {x}, y: {y}'
self.label.setText(text)
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec())
if __name__ == '__main__':
main()
本例中,在标签组件里,展示了鼠标的坐标。
self.setMouseTracking(True)
鼠标跟踪默认是关闭的,鼠标移动时,组件只能在鼠标按下的时候接收到事件。开启鼠标跟踪,只移动鼠标不按下鼠标按钮,也能接收到事件。
def mouseMoveEvent(self, e):
x = int(e.position().x())
y = int(e.position().y())
...
e
是事件对象,它包含了事件触发时候的数据。通过 position().x()
和 e.position().y()
方法,能获取到鼠标的坐标值。
self.text = f'x: {x}, y: {y}'
self.label = QLabel(self.text, self)
坐标值 x 和 y 显示在 QLabel
组件里。
菜鸟实战,持续学习!