PyQt 5事件和信号
信号槽Signals & slots
sld.valueChanged.connect(lcd.display)
# 将滚动条的valueChanged信号连接到lcd的display插槽
1 # Author:凌逆战 2 # -*- coding:utf-8 -*- 3 import sys 4 from PyQt5.QtCore import Qt 5 from PyQt5.QtWidgets import QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication 6 7 class Example(QWidget): 8 def __init__(self): 9 super().__init__() 10 self.initUI() 11 12 def initUI(self): 13 lcd = QLCDNumber(self) # 创建一个lcd显示屏对象 14 sld = QSlider(Qt.Horizontal, self) #创建一个水平的滑块 15 16 vbox = QVBoxLayout() # 创建垂直布局 17 vbox.addWidget(lcd) # 在垂直布局中添加lcd显示屏 18 vbox.addWidget(sld) # 在垂直布局中添加水平滑块 19 20 self.setLayout(vbox) # 设置垂直布局 21 # 将滑块的valueChanged信号连接到lcd的display插槽 22 sld.valueChanged.connect(lcd.display) 23 24 self.setGeometry(300, 300, 250, 150) 25 self.setWindowTitle('Signal & slot') 26 self.show() 27 28 if __name__ == '__main__': 29 app = QApplication(sys.argv) 30 ex = Example() 31 sys.exit(app.exec_())
重新实现事件处理器
def keyPressEvent(self, e): if e.key() == Qt.Key_Escape: self.close()
# 重新实现了keyPressEvent()事件处理器
# 键盘按下Escape键会使程序退出
1 # Author:凌逆战 2 # -*- coding:utf-8 -*- 3 import sys 4 from PyQt5.QtCore import Qt 5 from PyQt5.QtWidgets import QWidget, QApplication 6 7 class Example(QWidget): 8 def __init__(self): 9 super().__init__() 10 self.initUI() 11 12 def initUI(self): 13 self.setGeometry(300, 300, 250, 150) 14 self.setWindowTitle('Event handler') 15 self.show() 16 17 def keyPressEvent(self, e): 18 if e.key() == Qt.Key_Escape: 19 self.close() 20 21 if __name__ == '__main__': 22 app = QApplication(sys.argv) 23 ex = Example() 24 sys.exit(app.exec_())
事件发送者
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
# 两个按钮绑定到同一个插槽
def buttonClicked(self): sender = self.sender() self.statusBar().showMessage(sender.text() + ' was pressed')
# 通过调用sender()方法来判断信号源, 并将其名称显示在窗体的状态栏中。
1 # Author:凌逆战 2 # -*- coding:utf-8 -*- 3 # -*- coding: utf-8 -*- 4 import sys 5 from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication 6 7 class Example(QMainWindow): 8 def __init__(self): 9 super().__init__() 10 self.initUI() 11 12 def initUI(self): 13 btn1 = QPushButton("Button 1", self) # 按钮一Button1 14 btn1.move(30, 50) 15 16 btn2 = QPushButton("Button 2", self) # 按钮二Button2 17 btn2.move(150, 50) 18 # 两个按钮绑定到一个插槽 19 btn1.clicked.connect(self.buttonClicked) # 按钮一绑定事件 20 btn2.clicked.connect(self.buttonClicked) # 按钮二绑定事件 21 22 self.statusBar() # 创建一个状态栏 23 24 self.setGeometry(300, 300, 290, 150) 25 self.setWindowTitle('Event sender') 26 self.show() 27 28 def buttonClicked(self): 29 sender = self.sender() # 通过调用sender()方法来判断信号源 30 self.statusBar().showMessage(sender.text() + ' was pressed') 31 32 if __name__ == '__main__': 33 app = QApplication(sys.argv) 34 ex = Example() 35 sys.exit(app.exec_())
发出信号
class Communicate(QObject): closeApp = pyqtSignal()
# 创建了一个名为closeApp的信号
self.c = Communicate()
self.c.closeApp.connect(self.close)
# 自定义closeApp信号连接到QMainWindow的close槽
def mousePressEvent(self, event): self.c.closeApp.emit()
# 当在窗体上点击鼠标时会触发closeApp信号,使程序退出。
1 # Author:凌逆战 2 # -*- coding:utf-8 -*- 3 import sys 4 from PyQt5.QtCore import pyqtSignal, QObject 5 from PyQt5.QtWidgets import QMainWindow, QApplication 6 # 创建一个closeAPP信号 7 class Communicate(QObject): 8 closeApp = pyqtSignal() 9 10 class Example(QMainWindow): 11 def __init__(self): 12 super().__init__() 13 self.initUI() 14 15 def initUI(self): 16 self.c = Communicate() 17 self.c.closeApp.connect(self.close) # 自定义closeAPP信号连接到QMainWindow的close槽 18 19 self.setGeometry(300, 300, 290, 150) 20 self.setWindowTitle('Emit signal') 21 self.show() 22 # 重写mousePressEvent函数 23 # 当在窗体上点击鼠标时会触发closeAPP信号,是程序退出 24 def mousePressEvent(self, event): 25 self.c.closeApp.emit() 26 27 if __name__ == '__main__': 28 app = QApplication(sys.argv) 29 ex = Example() 30 sys.exit(app.exec_())