1、通过窗口类的属性来传递参数
from PyQt5.QtWidgets import QDialogButtonBox, QDateTimeEdit, QDialog, QComboBox, QTableView, QAbstractItemView, \
QHeaderView, QTableWidget, QTableWidgetItem, QMessageBox, QListWidget, QListWidgetItem, QStatusBar, QMenuBar, QMenu, \
QAction, QLineEdit, QStyle, QFormLayout, QVBoxLayout, QWidget, QApplication, QHBoxLayout, QPushButton, QMainWindow, \
QGridLayout, QLabel
from PyQt5.QtGui import QIcon, QPixmap, QStandardItem, QStandardItemModel, QCursor, QFont, QBrush, QColor, QPainter, \
QMouseEvent, QImage, QTransform
from PyQt5.QtCore import QStringListModel, QAbstractListModel, QModelIndex, QSize, Qt, QObject, pyqtSignal, QTimer, \
QEvent, QDateTime, QDate
import sys
class Win(QWidget):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
self.resize(400, 400)
self.btn = QPushButton("按钮", self)
self.btn.move(50, 50)
self.btn.setMinimumWidth(80)
self.label = QLabel('显示信息', self)
self.label.setMinimumWidth(420)
self.btn.clicked.connect(self.fn)
def fn(self):
date, time, res = Child_Dialog.getResult(self)
print(date, time, res)
class Child_Dialog(QDialog):
def __init__(self, parent=None):
super(Child_Dialog, self).__init__(parent)
layout = QVBoxLayout(self)
self.label = QLabel(self)
self.datetime = QDateTimeEdit(self)
self.datetime.setCalendarPopup(True)
self.datetime.setDateTime(QDateTime.currentDateTime())
self.label.setText("请选择日期")
layout.addWidget(self.label)
layout.addWidget(self.datetime)
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
@staticmethod
def getResult(self, parent=None):
dialog = Child_Dialog(parent)
result = dialog.exec_()
d = dialog.datetime.dateTime()
return (d.date(), d.time(), result)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Win()
win.show()
sys.exit(app.exec_())
2、窗口之间使用自定义信号和槽机制传参
from PyQt5.QtWidgets import QDialogButtonBox, QDateTimeEdit,QDialog,QComboBox,QTableView,QAbstractItemView,QHeaderView,QTableWidget, QTableWidgetItem, QMessageBox,QListWidget,QListWidgetItem, QStatusBar, QMenuBar,QMenu,QAction,QLineEdit,QStyle,QFormLayout, QVBoxLayout,QWidget,QApplication ,QHBoxLayout, QPushButton,QMainWindow,QGridLayout,QLabel
from PyQt5.QtGui import QIcon,QPixmap,QStandardItem,QStandardItemModel,QCursor,QFont,QBrush,QColor,QPainter,QMouseEvent,QImage,QTransform
from PyQt5.QtCore import QStringListModel,QAbstractListModel,QModelIndex,QSize,Qt,QObject,pyqtSignal,QTimer,QEvent,QDateTime,QDate
import sys
class Win(QWidget):
def __init__(self,parent=None):
super(Win, self).__init__(parent)
self.resize(400,400)
self.btn=QPushButton("按钮",self)
self.btn.move(50,50)
self.btn.setMinimumWidth(120)
self.btn.clicked.connect(self.openDialog)
self.label=QLabel('这里显示信息',self)
self.label.setMinimumWidth(420)
def openDialog(self):
dialog=Child_Dialog(self)
dialog.datetime.dateChanged.connect(self.slot_inner)
dialog.dialogSignel.connect(self.slot_emit)
dialog.show()
def slot_inner(self,date):
print("主窗口:method_1")
self.label.setText("①"+str(date)+">>内置消息获取日期为")
def slot_emit(self,flag,str):
print("主窗口:method_2")
print(flag)
if flag=="0":
self.label.setText("②"+str(str)+">>自定义消息")
else:
self.label.setText(str)
class Child_Dialog(QDialog):
dialogSignel=pyqtSignal(int,str)
def __init__(self,parent=None):
super(Child_Dialog, self).__init__(parent)
layout=QVBoxLayout(self)
self.label=QLabel(self)
self.datetime=QDateTimeEdit(self)
self.datetime.setCalendarPopup(True)
self.datetime.setDateTime(QDateTime.currentDateTime())
self.label.setText("请选择日期")
layout.addWidget(self.label)
layout.addWidget(self.datetime)
buttons=QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel,Qt.Horizontal,self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
def accept(self):
print("accept")
self.dialogSignel.emit(0,self.datetime.text())
self.destroy()
def reject(self):
print('reject')
self.dialogSignel.emit(1,"清空")
self.destroy()
if __name__=='__main__':
app=QApplication(sys.argv)
win = Win()
win.show()
sys.exit(app.exec_())