pyqt5中的多窗口数据交互有多种方式:
方式1:直接访问控件数据—一般不推荐使用(代码的耦合度高)
方式2:通过信号来传输数据,推荐使用,代码的耦合度低,可操作性强
主窗口的代码:
#coding=utf8
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
"""
多窗口交互---直接访问对应的控件,代码的耦合度高
方式1 直接访问控件
"""
# from DataDialog import DateDialog
#
# class MultiWindow1(QWidget):
# def __init__(self):
# super().__init__()
# self.setWindowTitle(" 多窗口交互")
#
# self.lineEdit = QLineEdit(self)
# self.button1 = QPushButton('弹出对话框1')
# self.button1.clicked.connect(self.onButton1Click)
#
# self.button2 = QPushButton('弹出对话框2')
# self.button2.clicked.connect(self.onButton2Click)
#
# gridLayout = QGridLayout()
# gridLayout.addWidget(self.button1)
# gridLayout.addWidget(self.button2)
# gridLayout.addWidget(self.lineEdit)
#
# self.setLayout(gridLayout)
#
# def onButton1Click(self):
# dialog = DateDialog(self)
# # 对话框保持悬浮
# result = dialog.exec()
# date = dialog.dateTime()
# self.lineEdit.setText(date.date().toString())
# dialog.destroy()
#
# def onButton2Click(self):
# date,time,result = DateDialog.getDateTime()
# self.lineEdit.setText(date.toString())
# if result == QDialog.Accepted:
# print("点击确定按钮")
# else:
# print('单击取消按钮')
"""
多窗口交互,使用信号与槽函数
这样就可以降低代码的耦合度: 信号:创建、触发、连接
"""
from DataDialog import DateDialog
class MultiWindow1(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle(" 多窗口交互")
self.lineEdit = QLineEdit(self)
self.button1 = QPushButton('弹出对话框1')
self.button1.clicked.connect(self.onButton1Click)
self.button2 = QPushButton('弹出对话框2')
self.button2.clicked.connect(self.onButton2Click)
gridLayout = QGridLayout()
gridLayout.addWidget(self.button1)
gridLayout.addWidget(self.button2)
gridLayout.addWidget(self.lineEdit)
self.setLayout(gridLayout)
def onButton1Click(self):
dialog = DateDialog(self)
"""
这是直接访问
"""
# date = dialog.dateTime()
# self.lineEdit.setText(date.date().toString())
"""
使用信号访问,信号连接槽,,多窗口交互的时候尽量使用信号来传数据--尽量避免直接访问控件
创建---在子窗口
触发---在子窗口
信号一旦触发就会执行绑定的槽函数------所以说用信号来传输数据是最佳的载体
绑定---在主窗口
"""
# 绑定
dialog.Signal_parp.connect(self.deal_emit_slot)
dialog.show()
# dialog.destroy()
def deal_emit_slot(self,datestr):
# self.lineEdit.setText("1234")
self.lineEdit.setText(datestr)
def onButton2Click(self):
date,time,result = DateDialog.getDateTime()
self.lineEdit.setText(date.toString())
if result == QDialog.Accepted:
print("点击确定按钮")
else:
print('单击取消按钮')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MultiWindow1()
window.show()
app.exec_()
子窗口的代码(使用子窗口的控件访问数据,使用子窗口的信号传输数据)
#coding=utf8
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class DateDialog(QDialog):
# 信号的定义
Signal_parp = pyqtSignal(str)
def __init__(self,parent=None):
super(DateDialog,self).__init__(parent)
self.setWindowTitle("DataDialog")
layout = QVBoxLayout(self)
self.datetime = QDateTimeEdit(self)
self.datetime.setCalendarPopup(True)
self.datetime.setDateTime(QDateTime.currentDateTime())
layout.addWidget(self.datetime)
button = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel,Qt.Horizontal,self)
button.accepted.connect(self.accept)
button.rejected.connect(self.reject)
layout.addWidget(button)
self.button = QPushButton("发送")
# self.button.clicked.connect(self.onClickButton)
# 日历控件时间改变的事件和之前的clicked事件是一样的都需要绑定槽函数
self.datetime.dateTimeChanged.connect(self.onClickButton)
# 使用按键触发---来导致信号触发
self.button.clicked.connect(self.buttonClicked)
layout.addWidget(self.button)
# 信号的触发可以通过其他控件来触发----控件本身就有触发机制
def onClickButton(self):
data_str = self.datetime.dateTime().toString()
print("触发了")
print(data_str)
# 信号的触发
self.Signal_parp.emit(data_str)
# 按键触发---->导致信号触发试试
def buttonClicked(self):
data_str = self.datetime.dateTime().toString()
self.Signal_parp.emit(data_str)
def dateTime(self):
return self.datetime.dateTime()
@staticmethod
def getDateTime(parent = None):
dialog = DateDialog(parent)
result = dialog.exec()
date = dialog.dateTime()
return (date.date(),date.time(),result == QDialog.Accepted)
直接测试使用,推荐使用信号来进行数据交互