本文代码来自zetcode.com
GUI应用是以事件驱动,事件的产生主要靠用户。
但是事件也可以通过例如互联网链接、窗口管理器或者计时器产生。
当我们通过exec_()方法进入程序主循环,主循环获取事件后并将其发送。
在事件模型中,有三个参与者:
event source
event object
event target
event source 状态发生改变时产生事件
event object 在事件源中封装改变的状态
event target 事件目标是想要通知的对象
PyQt5具有特有的信号和插槽机制来处理事件:
当事件发生会产生一个 signal,当连接信号发送时,slot被调用。
#!/usr/bin/python3
"""
This program will create a signal
of a QSlider to a slot of a QLCDNumber
"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QLCDNumber, QSlider, QVBoxLayout
from PyQt5.QtCore import Qt
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 and slot')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
上述代码展示了QtGui.QLCDNumber和QtGui.QSlider,当我们拖拽滑块数字也随之改变。
sld.valueChanged.connect(lcd.display)
以下代码实现Esc键退出程序
#!/usr/bin/python3
"""
In this example, we reimplement an event handler.
"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt
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.Kye_Escape:
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
我们重新实现keyPressEvent()事件处理程序
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
self.close()
事件对象是包含许多描述事件的属性,事件对象特定于生成的事件类型。
本例实现移动鼠标展示其所在坐标位置
#!/usr/bin/python3
"""
In this example, we display the x and y
coordinates of a mouse pointer in a label widget.
"""
import sys
from PyQt5.QtCore import Qt
from PyQt5.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 = "x: {0}, y: {1}".format(x, y)
self.label = QLabel(self.text, self) # (x,y)坐标在QLabel控件中显示
grid.addWidget(self.label, 0, 0, Qt.AlignTop) # 将控件放于左上角
self.setMouseTracking(True) # 默认为关闭,开启后事件产生信号
self.setLayout(grid)
self.setGeometry(300, 300, 350, 200)
self.setWindowTitle('Event Object')
self.show()
def mouseMoveEvent(self, e):
x = e.x()
y = e.y()
text = "x: {0}. y: {1}".format(x, y)
self.label.setText(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
sender()方法可以知道哪个控件发送信号
#!/usr/bin/python3
"""
In this example, we determine the event sender object.
"""
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被按下,状态栏会显示提示消息。
sender = self.sender()
通过sender()来知晓信号源
从QObject创建的对象可以发送信号
#!/usr/bin/python3
"""
In this example, we show how to emit a custom signal.
"""
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, 300, 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_())
信号通过pyqtSignal()创建,自定义的closeApp信号连接到QMainWindow的close()插槽。
self.c = Communicate()
self.c.closeApp.connect(self.close)
我们定义了鼠标点击事件,当我们点击鼠标时,程序退出
def mousePressEvent(self, event):
self.c.closeApp.emit()