pyqt5中sender方法介绍_【第五节】PyQt5事件和信号
在這一部分的pyqt5教程中,我們將探討PyQt5中的事件Event
事件 Event
所有的GUI程序都是事件驅動的。事件主要由用戶觸發,但也可能有其他觸發方式:例如網絡連接、window manager或定時器。當我們調用QApplication的exec_()方法時會使程序進入主循環。主循環會獲取并分發事件。
在事件模型中,有三個參與者:
事件源
事件對象
事件接收者
事件源是狀態發生變化的對象。它會生成事件。事件(對象)封裝了事件源中狀態的變動。事件接收者是要通知的對象。事件源對象將事件處理的工作交給事件接收者。
PyQt5有一個獨特的signal&slot(信號槽)機制來處理事件。信號槽用于對象間的通信。signal在某一特定事件發生時被觸發,slot可以是任何callable對象。當signal觸發時會調用與之相連的slot。
信號槽 Signals & slots
這是一個使用信號槽的PyQt5例子。
Python
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Py40 PyQt5 tutorial
In this example, we connect a signal
of a QSlider to a slot of a QLCDNumber.
author: Jan Bodnar
website: py40.com
last edited: January 2015
"""
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider,
QVBoxLayout, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
lcd = QLCDNumber(self)
sld = QSlider(Qt.Horizontal, self)
vbox = QVBoxLayout()
vbox.addWidget(lcd)
vbox.addWidget(sld)
self.setLayout(vbox)
sld.valueChanged.connect(lcd.display)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal & slot')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
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
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Py40 PyQt5 tutorial
In this example, we connect a signal
of a QSlider to a slot of a QLCDNumber.
author: Jan Bodnar
website: py40.com
last edited: January 2015
"""
importsys
fromPyQt5.QtCoreimportQt
fromPyQt5.QtWidgetsimport(QWidget,QLCDNumber,QSlider,
QVBoxLayout,QApplication)
classExample(QWidget):
def__init__(self):
super().__init__()
self.initUI()
definitUI(self):
lcd=QLCDNumber(self)
sld=QSlider(Qt.Horizontal,self)
vbox=QVBoxLayout()
vbox.addWidget(lcd)
vbox.addWidget(sld)
self.setLayout(vbox)
sld.valueChanged.connect(lcd.display)
self.setGeometry(300,300,250,150)
self.setWindowTitle('Signal & slot')
self.show()
if__name__=='__main__':
app=QApplication(sys.argv)
ex=Example()
sys.exit(app.exec_())
這個例子中展示了一個QtGui.QLCDNumber和QtGui.QSlider。lcd的值會隨著滑塊的拖動而改變。
Python
sld.valueChanged.connect(lcd.display)
1
sld.valueChanged.connect(lcd.display)
在這里我們將滾動條的valueChanged信號連接到lcd的display插槽。
sender是發出信號的對象。receiver是接收信號的對象。slot(插槽)是對信號做出反應的方法。
重新實現事件處理器
在PyQt5中常通過重新實現事件處理器來處理事件。
Python
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
pyu40 PyQt5 tutorial
In this example, we reimplement an
event handler.
author: Jan Bodnar
website: py40.com
last edited: January 2015
"""
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Event handler')
self.show()
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
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
40
41
42
43
44
45
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
pyu40 PyQt5 tutorial
In this example, we reimplement an
event handler.
author: Jan Bodnar
website: py40.com
last edited: January 2015
"""
importsys
fromPyQt5.QtCoreimportQt
fromPyQt5.QtWidgetsimportQWidget,QApplication
classExample(QWidget):
def__init__(self):
super().__init__()
self.initUI()
definitUI(self):
self.setGeometry(300,300,250,150)
self.setWindowTitle('Event handler')
self.show()
defkeyPressEvent(self,e):
ife.key()==Qt.Key_Escape:
self.close()
if__name__=='__main__':
app=QApplication(sys.argv)
ex=Example()
sys.exit(app.exec_())
在示例中我們重新實現了keyPressEvent()事件處理器。
Python
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
self.close()
1
2
3
defkeyPressEvent(self,e):
ife.key()==Qt.Key_Escape:
self.close()
我們按下Escape鍵會使程序退出。
事件發送者
有時需要知道信號是由哪個控件發出的。對此PyQt5提供了sender()方法。
Python
# -*- coding: utf-8 -*-
"""
PyQt5 tutorial
In this example, we determine the event sender
object.
author: py40.com
last edited: 2017年3月
"""
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn1 = QPushButton("Button 1", self)
btn1.move(30, 50)
btn2 = QPushButton("Button 2", self)
btn2.move(150, 50)
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
self.statusBar()
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Event sender')
self.show()
def buttonClicked(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
PyQt5 tutorial
In this example, we determine the event sender
object.
author: py40.com
last edited: 2017年3月
"""
importsys
fromPyQt5.QtWidgetsimportQMainWindow,QPushButton,QApplication
classExample(QMainWindow):
def__init__(self):
super().__init__()
self.initUI()
definitUI(self):
btn1=QPushButton("Button 1",self)
btn1.move(30,50)
btn2=QPushButton("Button 2",self)
btn2.move(150,50)
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
self.statusBar()
self.setGeometry(300,300,290,150)
self.setWindowTitle('Event sender')
self.show()
defbuttonClicked(self):
sender=self.sender()
self.statusBar().showMessage(sender.text()+' was pressed')
if__name__=='__main__':
app=QApplication(sys.argv)
ex=Example()
sys.exit(app.exec_())
我們創建了兩個按鈕。在buttonClicked()方法中通過調用sender()方法來判斷當前按下的是哪個按鈕。
Python
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
1
2
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
兩個按鈕連接到了同一個插槽。
Python
def buttonClicked(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
1
2
3
4
defbuttonClicked(self):
sender=self.sender()
self.statusBar().showMessage(sender.text()+' was pressed')
我們通過調用sender()方法來判斷信號源, 并將其名稱顯示在窗體的狀態欄中。
發出信號
通過QObject創建的對象可以發出信號。下面的示例演示了如何發出自定義信號
Python
# -*- coding: utf-8 -*-
"""
PyQt5 tutorial
In this example, we determine the event sender
object.
author: py40.com
last edited: 2017年3月
"""
import sys
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QMainWindow, QApplication
class Communicate(QObject):
closeApp = pyqtSignal()
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.c = Communicate()
self.c.closeApp.connect(self.close)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Emit signal')
self.show()
def mousePressEvent(self, event):
self.c.closeApp.emit()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
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
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
PyQt5 tutorial
In this example, we determine the event sender
object.
author: py40.com
last edited: 2017年3月
"""
importsys
fromPyQt5.QtCoreimportpyqtSignal,QObject
fromPyQt5.QtWidgetsimportQMainWindow,QApplication
classCommunicate(QObject):
closeApp=pyqtSignal()
classExample(QMainWindow):
def__init__(self):
super().__init__()
self.initUI()
definitUI(self):
self.c=Communicate()
self.c.closeApp.connect(self.close)
self.setGeometry(300,300,290,150)
self.setWindowTitle('Emit signal')
self.show()
defmousePressEvent(self,event):
self.c.closeApp.emit()
if__name__=='__main__':
app=QApplication(sys.argv)
ex=Example()
sys.exit(app.exec_())
我們創建了一個名為closeApp的信號。這個信號會在按下鼠標時觸發,它連接著QMainWindow的close()插槽。
Python
class Communicate(QObject):
closeApp = pyqtSignal()
1
2
classCommunicate(QObject):
closeApp=pyqtSignal()
信號closeApp是Communicate的類屬性,它由pyqtSignal()創建。
Python
self.c = Communicate()
self.c.closeApp.connect(self.close)
1
2
self.c=Communicate()
self.c.closeApp.connect(self.close)
自定義closeApp信號連接到QMainWindow的close槽
Python
def mousePressEvent(self, event):
self.c.closeApp.emit()
1
2
3
defmousePressEvent(self,event):
self.c.closeApp.emit()
當在窗體上點擊鼠標時會觸發closeApp信號,使程序退出。
總結
以上是生活随笔為你收集整理的pyqt5中sender方法介绍_【第五节】PyQt5事件和信号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: osg画线_osg 基本几何图元
- 下一篇: cmenu 隐藏子项中的一个子项_QML