PyQt5基础学习-两个界面进行交互(不使用信号传递)

1.方法1: 通过对类进行执行, 然后获得类的执行的结果

方法2:通过调用类里面的静态方法,对类进行执行来获得最终的结果
DateDialog.py

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class DateDialog(QDialog):
    def __init__(self, parent=None):
        super(DateDialog, self).__init__(parent)
        self.setWindowTitle("DateDialog")

        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)

    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==QDialogButtonBox.Ok)

MultiWindow1.py

"""
多窗口交互(1):不使用信号和槽
win1
win2
"""
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from DateDialog import DateDialog

class MultiWindow1(QWidget):
    def __init__(self):
        super(MultiWindow1, self).__init__()
        self.setWindowTitle("多窗口交互(1):不使用信号和槽")

        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.lineEdit)
        gridLayout.addWidget(self.button1)
        gridLayout.addWidget(self.button2)

        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("点击取消按钮")

if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = MultiWindow1()
    main.show()

    sys.exit(app.exec_())

PyQt5基础学习-两个界面进行交互(不使用信号传递)

 

上一篇:论静态方法和类方法


下一篇:033-Python入门(面向对象基础知识)-魔法方法inin,str,del