#窗口之間數據傳遞(通過屬性方式) 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) #打開Dialog def openDialog(self): dialog=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":#點擊ok self.label.setText("②"+str(str)+">>自定義消息") else:#點擊cancel self.label.setText(str) #彈出框對象 class Dialog(QDialog): #自定義消息 dialogSignel=pyqtSignal(int,str) def __init__(self,parent=None): super(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)#點擊ok buttons.rejected.connect(self.reject)#點擊cancel layout.addWidget(buttons) def accept(self):#點擊ok是發送內置信號 print("accept") self.dialogSignel.emit(0,self.datetime.text()) self.destroy() def reject(self):#點擊cancel時,發送自定義信號 print('reject') self.dialogSignel.emit(1,"清空") self.destroy() if __name__=='__main__': app=QApplication(sys.argv) win = Win() win.show() sys.exit(app.exec_())