Pyqt5 简明教程
PyQt5 - Signals & Slots
与按顺序执行的控制台模式应用程序不同,基于 GUI 的应用程序是事件驱动的。函数或方法会执行响应用户的操作,例如单击按钮、从集合中选择一个项目,或鼠标单击等,称为 events 。
Unlike a console mode application, which is executed in a sequential manner, a GUI based application is event driven. Functions or methods are executed in response to user’s actions like clicking on a button, selecting an item from a collection or a mouse click etc., called events.
用于构建 GUI 界面并担任这些事件来源的控件。每个 PyQt 控件都派生自 QObject 类,设计为针对一个或多个事件发出 ‘signal’ 。信号本身不执行任何操作。相反,它“连接”到 ‘slot’ 。槽可以是任何 callable Python function 。
Widgets used to build the GUI interface act as the source of such events. Each PyQt widget, which is derived from QObject class, is designed to emit ‘signal’ in response to one or more events. The signal on its own does not perform any action. Instead, it is ‘connected’ to a ‘slot’. The slot can be any callable Python function.
Using Qt Designer’s Signal/Slot Editor
先使用 LineEdit 控件和 PushButton 设计一个简单的窗体。
First design a simple form with a LineEdit control and a PushButton.

期望在按下按钮时擦除文本框中的内容。QLineEdit 控件为此提供了一个 clear() 方法。因此,按钮的 clicked 信号要连接到文本框的 clear() 方法。
It is desired that if button is pressed, contents of text box should be erased. The QLineEdit widget has a clear() method for this purpose. Hence, the button’s clicked signal is to be connected to clear() method of the text box.
首先,从 Edit 菜单选择 Edit signals/slots(或按 F4)。然后用鼠标高亮显示按钮,并拖动光标指向文本框
To start with, choose Edit signals/slots from Edit menu (or press F4). Then highlight the button with mouse and drag the cursor towards the textbox

当释放鼠标时,将显示一个对话框,其中显示按钮的信号和槽的方法。选择 clicked 信号和 clear() 方法
As the mouse is released, a dialog showing signals of button and methods of slot will be displayed. Select clicked signal and clear() method

右下角的 Signal/Slot Editor 窗口将显示结果 −
The Signal/Slot Editor window at bottom right will show the result −

按以下代码所示从 ui 文件中保存 ui 和构建 Python 代码 −
Save ui and Build and Python code from ui file as shown in the below code −
pyuic5 -x signalslot.ui -o signalslot.py
生成的 Python 代码将通过以下语句在信号和槽之间建立连接 −
Generated Python code will have the connection between signal and slot by the following statement −
self.pushButton.clicked.connect(self.lineEdit.clear)
运行 signalslot.py,然后在 LineEdit 中输入一些文本。如果按下按钮,文本将被清除。
Run signalslot.py and enter some text in the LineEdit. The text will be cleared if the button is pressed.
Building Signal-slot Connection
除了使用 Designer,你还可以按照以下语法直接建立信号-槽连接 −
Instead of using Designer, you can directly establish signal-slot connection by following syntax −
widget.signal.connect(slot_function)
假设在按下按钮时需要调用一个函数。在这里,clicked 信号要连接到一个可调用函数。它可以通过以下任何技术实现 −
Suppose if a function is to be called when a button is clicked. Here, the clicked signal is to be connected to a callable function. It can be achieved in any of the following technique −
button.clicked.connect(slot_function)
Example
在以下示例中,两个 QPushButton 对象(b1 和 b2)添加到 QDialog 窗口中。我们希望分别在单击 b1 和 b2 时调用函数 b1_clicked() 和 b2_clicked()。
In the following example, two QPushButton objects (b1 and b2) are added in QDialog window. We want to call functions b1_clicked() and b2_clicked() on clicking b1 and b2 respectively.
当单击 b1 时,clicked() 信号连接到 b1_clicked() 函数 −
When b1 is clicked, the clicked() signal is connected to b1_clicked() function −
b1.clicked.connect(b1_clicked())
当单击 b2 时,clicked() 信号连接到 b2_clicked() 函数。
When b2 is clicked, the clicked() signal is connected to b2_clicked() function.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
def window():
app = QApplication(sys.argv)
win = QDialog()
b1 = QPushButton(win)
b1.setText("Button1")
b1.move(50,20)
b1.clicked.connect(b1_clicked)
b2 = QPushButton(win)
b2.setText("Button2")
b2.move(50,50)
b2.clicked.connect(b2_clicked)
win.setGeometry(100,100,200,100)
win.setWindowTitle("PyQt5")
win.show()
sys.exit(app.exec_())
def b1_clicked():
print ("Button 1 clicked")
def b2_clicked():
print ("Button 2 clicked")
if __name__ == '__main__':
window()
上述代码生成以下输出 -
The above code produces the following output −
