forked from muziing/PyQt_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-PyQt事件机制.py
39 lines (25 loc) · 880 Bytes
/
09-PyQt事件机制.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sys
from PyQt5.Qt import *
class APP(QApplication):
"""重写了QApplication类,捕捉并显示事件"""
def notify(self, receiver, evt):
if receiver.inherits("QPushButton") and evt.type() == QEvent.MouseButtonPress:
print(receiver, evt)
return super().notify(receiver, evt) # 负责分发
class Btn(QPushButton):
def event(self, evt):
if evt.type() == QEvent.MouseButtonPress:
print(evt)
return super().event(evt)
def mousePressEvent(self, *args, **kwargs) -> None:
print("鼠标被按下了……")
return super().mousePressEvent(*args, **kwargs)
app = APP(sys.argv)
window = QWidget()
window.resize(500, 500)
btn = Btn(window)
btn.setText("按钮")
btn.move(200, 200)
btn.pressed.connect(lambda: print("cao-按钮被点击了"))
window.show()
sys.exit(app.exec_())