所有的应用都是由事件驱动的,事件大部分都是由用户的行为产生的。调用应用的exec_()方法时,应用会进入主循环,主循环会监听和分发事件
在事件模型中,分为三个角色:
事件源绑定事件处理函数,然后作用于事件目标身上
PyQt5处理事件方面有个signal and slot机制,Signals and slots用于对象间的通讯。事件触发的时候,发生一个signal,是Python调用一个slot(事件)
程序展示
本例中,显示了QtWidgets.QLCDNumber
和QtWidgets.QSlider
模块,我们能拖动滑块让数字跟着发生改变
import sys
from PyQt5.QtWidgets import QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication
from PyQt5.QtCore import Qt
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
lcd = QLCDNumber(self)
slider = QSlider(Qt.Horizontal, self)
vbox = QVBoxLayout()
vbox.addWidget(lcd)
vbox.addWidget(slider)
self.setLayout(vbox)
slider.valueChanged.connect(lcd.display)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal and slot')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
e = Example()
sys.exit(app.exec_())
程序预览:
代码解释
创建LCD数字对象,显示数字
lcd = QLCDNumber(self)
创建滑块对象,Qt.Horizontal
使滑块水平放置
slider = QSlider(Qt.Horizontal, self)
把滑块的变化和数字的变化绑定在一起
slider.valueChanged.connect(lcd.display)
程序展示
这个例子中,重写了事件处理器函数keyPressEvent()
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, event):
if event.key() == Qt.Key_Space:
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
e = Example()
sys.exit(app.exec_())
代码解释
此时如果按下空格键
程序就会退出
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space:
self.close()
事件对象是用python来描述一系列的事件自身属性的对象
程序展示
本例中,在一个组件里显示鼠标的X和Y坐标
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QLabel
class Example(QWidget):
def __init__(self):
super().__init__()
x = y = 0
pos = f"x: {x}, y: {y}"
self.label = QLabel(pos, self)
self.initUI()
def initUI(self):
grid = QGridLayout()
grid.setSpacing(10)
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, event):
x, y = event.x(), event.y()
pos = f"x: {x}, y: {y}"
self.label.setText(pos)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
代码解释
x,y坐标显示在QLabel
组件里
x = y = 0
pos = f"x: {x}, y: {y}"
self.label = QLabel(pos, self)
栅格布局
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(self.label, 0, 0, Qt.AlignTop)
事件追踪默认没有开启,当开启后才会追踪鼠标的移动事件
self.setMouseTracking(True)
event
代表了事件对象。里面有我们触发事件(鼠标移动)的事件对象。x()
和y()
方法得到鼠标的x和y坐标点,然后拼成字符串输出到QLabel
组件里
def mouseMoveEvent(self, event):
x, y = event.x(), event.y()
pos = f"x: {x}, y: {y}"
self.label.setText(pos)
程序展示
有时候我们想知道是哪个组件发出了一个信号,PyQt5里的sender()
方法能搞定这件事。
本例中,创建了按钮,buttonclick()
方法决定了是哪个按钮能调用sender()
方法
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.bar = self.statusBar()
self.initUI()
def initUI(self):
b1 = QPushButton('button1', self)
b2 = QPushButton('button2', self)
b1.move(50, 50)
b2.move(150, 50)
b1.clicked.connect(self.buttonclick)
b2.clicked.connect(self.buttonclick)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('事件发送')
self.show()
def buttonclick(self):
sender = self.sender()
self.bar.showMessage(sender.text() + " is pressed")
if __name__ == '__main__':
app = QApplication(sys.argv)
e = Example()
sys.exit(app.exec_())
代码解释
两个按钮都和同一个slot绑定
btn1.clicked.connect(self.buttonclick)
btn2.clicked.connect(self.buttonclick)
用调用sender()
方法的方式决定了事件源。状态栏显示了被点击的按钮
def buttonclick(self):
sender = self.sender()
self.bar.showMessage(sender.text() + ' is pressed')
下一篇 事件和信号(二)