最近在探索pyqt5的多窗口通信,刚开始一直没有搞懂信号量与槽函数是怎么绑定的,今天终于弄懂了,并做了个小实验,记录一下
(一)信号量与槽函数
我的理解就是:
1.信号量就相当于一个信号,一个信号量可以和多个槽函数绑定,当你给发射信号时,与其绑定的槽函数都会接收到该信号并执行相应的操作。
2.槽函数就是一个功能函数,你可以在函数里实现各种功能,当与之绑定的信号量发射信号时,该函数就执行其功能。
(二)实验
为了加深理解,做了个小实验,实现三个窗口之间的通信,其中两个窗口的类型是mainwindow,另一个是dialogwindow。
1.代码部分
(1)win_1 type:mainwindow
class my_win_1(QtWidgets.QMainWindow, Ui_mywin_1):
# 定义信号量,为字符串类型
signal_1 = pyqtSignal(str)
def __init__(self):
super(my_win_1, self).__init__()
self.setupUi(self)
# 点击鼠标触发事件:点击鼠标发送信息
self.pushButton.clicked.connect(self.send_data)
# 创建接收信息的窗体对象
self.receive_win_2 = my_win_2()
# 将发送方的信号量和接收方的get_data槽函数绑定
self.signal_1.connect(self.receive_win_2.get_data)
self.receive_win_2.show()
# receive_win_2与自己的接收信息的槽函数绑定
self.receive_win_2.signal_2.connect(self.get_data)
self.receive_win_3 = my_win_3()
self.signal_1.connect(self.receive_win_3.get_data)
self.receive_win_3.show()
self.receive_win_3.signal_3.connect(self.get_data)
# 2号3号窗口之间通信
# 将2号窗口的信号量与3号窗口的get_data槽函数绑定
self.receive_win_2.signal_2.connect(self.receive_win_3.get_data)
# 将3号窗口的信号量与2号窗口的get_data槽函数绑定
self.receive_win_3.signal_3.connect(self.receive_win_2.get_data)
def send_data(self):
# 获取发送框的内容并发送信息
self.signal_1.emit(self.textEdit_send.toPlainText())
def get_data(self, data):
# 将接收到的信息显示到接收框中
self.textEdit_receive.append(data)
(2)win_2 type:mainwindow
class my_win_2(QtWidgets.QMainWindow, Ui_mywin_2):
# 创建信号量,为字符串类型
signal_2 = pyqtSignal(str)
def __init__(self):
super(my_win_2, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.send_data)
def send_data(self):
# 获取发送框的内容并发送信息
self.signal_2.emit(self.textEdit_send.toPlainText())
def get_data(self, data):
self.textEdit_receive.append(data)
(3)win_3 type:dialogwindow
class my_win_3(QtWidgets.QDialog, Ui_mywin_3):
signal_3 = pyqtSignal(str)
def __init__(self):
super(my_win_3, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.send_data)
def send_data(self):
self.signal_3.emit(self.textEdit_send.toPlainText())
def get_data(self, data):
self.textEdit_receive.append(data)
2.结果截图
win_1发送信息“111”,win_2,win_3接收到信息:
win_2发送信息“222”,win_1,win_3接收信息:
win_3发送信息“333”,win_1,win_2接收信息:
3.一些说明
在做这个小实验的过程中,我也试错了几次,在此说明一下:
(1)在实现多窗口通信的时候,你可以想象成以其中一个为主,其余为辅,即主从关系(或者父子关系),就是在“主窗口”中添加“从窗口”,就像win_1代码中的
self.receive_win_2 = my_win_2()
self.receive_win_3 = my_win_3()
创建了2号和3号两个“从窗口”。不要在“从窗口”中定义“主窗口”的对象,即不要在“从窗口”的类定义中出现像
self.receive_win = my_win_1()
这样的语句,这样会报错(我试了)
我原本想在“从窗口”的类中定义“主窗口”的对象,然后在“从窗口”的类中,将“从窗口”的信号量与这个对象的槽函数绑定,实验证明会报错。
(2)在执行的时候,只用定义一个窗体即可,即一直遵从“主从原则”,即在main中只用写一个创建窗体:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = my_win_1()
win.show()
sys.exit(app.exec_())
“从窗口”的显示在“主窗口”类定义中通过调用show()函数实现,即不要这样写:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = my_win_1()
win.show()
sys.exit(app.exec_())
app2 = QtWidgets.QApplication(sys.argv)
win2 = my_win_2()
win2.show()
sys.exit(app2.exec_())
不要在main中创建两个窗体,这样执行的时候,虽然win_1发送了信息,但是win_2收不到,我没有仔细研究这是为什么,但我觉得可能和进程什么的有关吧。
刚刚搜了一下,发现了一篇可以参考的博客:链接
(3)关于信号量与槽函数的绑定,就像前面说的,一个信号量可以和多个槽函数绑定。
就拿本实验来说,三个信号量signal_1, signal_2, signal_3,槽函数为send_data, receive_data,那么怎么绑定才能实现信息的发送和接收呢?首先我觉得信息的发送比较好理解,比如这段代码:
self.receive_win_2 = my_win_2()
self.signal_1.connect(self.receive_win_2.get_data)
这里就将win_1的信号量与win_2的槽函数get_data绑定了,如果win_1发射信号了,那么win_2的槽函数get_data就会执行相应的操作。
那实现win_2发射信号win_1接收信息:
self.receive_win_2.signal_2.connect(self.get_data)
这里就将win_2的信号量与win_1的槽函数get_data绑定了,那么当win_2发射信号时,win_1的槽函数get_data就会执行相应的操作。
同理,win_2和win_3的通信就通过以下代码实现:
self.receive_win_2.signal_2.connect(self.receive_win_3.get_data)
self.receive_win_3.signal_3.connect(self.receive_win_2.get_data)
4.总体实现
为了方便大家参考,在此附上整个实验的代码,我的文件结构如下:
其中ui是窗体文件
(1)win_1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mywin_1(object):
def setupUi(self, mywin_1):
mywin_1.setObjectName("mywin_1")
mywin_1.resize(701, 457)
self.centralwidget = QtWidgets.QWidget(mywin_1)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.verticalLayout.addWidget(self.label_2)
self.textEdit_send = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_send.setObjectName("textEdit_send")
self.verticalLayout.addWidget(self.textEdit_send)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.textEdit_receive = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_receive.setObjectName("textEdit_receive")
self.verticalLayout.addWidget(self.textEdit_receive)
mywin_1.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(mywin_1)
self.statusbar.setObjectName("statusbar")
mywin_1.setStatusBar(self.statusbar)
self.retranslateUi(mywin_1)
QtCore.QMetaObject.connectSlotsByName(mywin_1)
def retranslateUi(self, mywin_1):
_translate = QtCore.QCoreApplication.translate
mywin_1.setWindowTitle(_translate("mywin_1", "mainwin_1"))
self.label_2.setText(_translate("mywin_1", "在此输入要发送的信息"))
self.pushButton.setText(_translate("mywin_1", "发送"))
self.label.setText(_translate("mywin_1", "接收到的信息"))
(2)win_2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mywin_2(object):
def setupUi(self, mywin_2):
mywin_2.setObjectName("mywin_2")
mywin_2.resize(696, 464)
self.centralwidget = QtWidgets.QWidget(mywin_2)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 0, 1, 1)
self.textEdit_receive = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_receive.setObjectName("textEdit_receive")
self.gridLayout.addWidget(self.textEdit_receive, 5, 0, 1, 1)
self.textEdit_send = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_send.setObjectName("textEdit_send")
self.gridLayout.addWidget(self.textEdit_send, 1, 0, 1, 1)
mywin_2.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(mywin_2)
self.statusbar.setObjectName("statusbar")
mywin_2.setStatusBar(self.statusbar)
self.retranslateUi(mywin_2)
QtCore.QMetaObject.connectSlotsByName(mywin_2)
def retranslateUi(self, mywin_2):
_translate = QtCore.QCoreApplication.translate
mywin_2.setWindowTitle(_translate("mywin_2", "MainWindow"))
self.label.setText(_translate("mywin_2", "接收到的信息"))
self.pushButton.setText(_translate("mywin_2", "发送"))
self.label_2.setText(_translate("mywin_2", "在此输入要发送的信息"))
(3)win_3.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mywin_3(object):
def setupUi(self, mywin_3):
mywin_3.setObjectName("mywin_3")
mywin_3.resize(709, 465)
self.gridLayout = QtWidgets.QGridLayout(mywin_3)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(mywin_3)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(mywin_3)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.textEdit_send = QtWidgets.QTextEdit(mywin_3)
self.textEdit_send.setObjectName("textEdit_send")
self.gridLayout.addWidget(self.textEdit_send, 1, 0, 1, 1)
self.textEdit_receive = QtWidgets.QTextEdit(mywin_3)
self.textEdit_receive.setObjectName("textEdit_receive")
self.gridLayout.addWidget(self.textEdit_receive, 4, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(mywin_3)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 0, 1, 1)
self.retranslateUi(mywin_3)
QtCore.QMetaObject.connectSlotsByName(mywin_3)
def retranslateUi(self, mywin_3):
_translate = QtCore.QCoreApplication.translate
mywin_3.setWindowTitle(_translate("mywin_3", "Dialogwin_3"))
self.pushButton.setText(_translate("mywin_3", "发送"))
self.label.setText(_translate("mywin_3", "接收到的信息"))
self.label_2.setText(_translate("mywin_3", "在此输入要发送的信息"))
(4)mainwin.py
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSignal
from win_1 import Ui_mywin_1
from win_2 import Ui_mywin_2
from win_3 import Ui_mywin_3
class my_win_1(QtWidgets.QMainWindow, Ui_mywin_1):
# 定义信号量,为字符串类型
signal_1 = pyqtSignal(str)
def __init__(self):
super(my_win_1, self).__init__()
self.setupUi(self)
# 点击鼠标触发事件:点击鼠标发送信息
self.pushButton.clicked.connect(self.send_data)
# 创建接收信息的窗体对象
self.receive_win_2 = my_win_2()
# 将发送方的信号量和接收方的get_data绑定
self.signal_1.connect(self.receive_win_2.get_data)
# self.receive_win_2.show()
# receive_win_2与自己的接收信息的槽函数绑定
self.receive_win_2.signal_2.connect(self.get_data)
self.receive_win_3 = my_win_3()
self.signal_1.connect(self.receive_win_3.get_data)
self.receive_win_3.show()
self.receive_win_3.signal_3.connect(self.get_data)
# 2号3号窗口之间通信
# 将2号窗口的信号量与3号窗口的get_data槽函数绑定
self.receive_win_2.signal_2.connect(self.receive_win_3.get_data)
# 将3号窗口的信号量与2号窗口的get_data槽函数绑定
self.receive_win_3.signal_3.connect(self.receive_win_2.get_data)
def send_data(self):
# 获取发送框的内容并发送信息
self.signal_1.emit(self.textEdit_send.toPlainText())
def get_data(self, data):
# 将接收到的信息显示到接收框中
self.textEdit_receive.append(data)
class my_win_2(QtWidgets.QMainWindow, Ui_mywin_2):
# 创建信号量,为字符串类型
signal_2 = pyqtSignal(str)
def __init__(self):
super(my_win_2, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.send_data)
def send_data(self):
# 获取发送框的内容并发送信息
self.signal_2.emit(self.textEdit_send.toPlainText())
def get_data(self, data):
self.textEdit_receive.append(data)
class my_win_3(QtWidgets.QDialog, Ui_mywin_3):
signal_3 = pyqtSignal(str)
def __init__(self):
super(my_win_3, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.send_data)
def send_data(self):
self.signal_3.emit(self.textEdit_send.toPlainText())
def get_data(self, data):
self.textEdit_receive.append(data)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = my_win_1()
win.show()
sys.exit(app.exec_())