pyqt5 从 parent window 关闭 child window

pyqt5 closing a child window from a parent window

我使用 qt 设计器创建了 2 个 windows、win1win2,并添加了两个按钮:btn_open_win2 从 [ 打开 win2 =16=] 和 btn_close 关闭 win2.

当我 运行 win2 直接 close 方法正常工作但是当我 运行 win1 并从它调用 win2 时, close win2 上的按钮无法正常运行。

我知道问题出在 if __name__ =='__main__' 部分中定义了 win2,如下面的代码所示,但我无法解决这个问题。

win1:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

class Ui_Win1(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("Win1")
        MainWindow.resize(640, 480)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.btn_open_win2 = QtWidgets.QPushButton(self.centralwidget)
        self.btn_open_win2.setGeometry(QtCore.QRect(250, 170, 131, 101))
        self.btn_open_win2.setObjectName("btn_open_win2")
        self.btn_open_win2.clicked.connect(self.on_open_win2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Win1"))
        self.btn_open_win2.setText(_translate("MainWindow", "open win2"))

    def on_open_win2(self):
        from win2 import Ui_Win2
        self.win = QMainWindow()
        self.ui = Ui_Win2()
        self.ui.setupUi(self.win)
        self.win.show()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win1 = QMainWindow()
    ui = Ui_Win1()
    ui.setupUi(win1)
    win1.show()
    sys.exit(app.exec_())

win2:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

class Ui_Win2(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("Win2")
        MainWindow.resize(640, 480)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.btn_close = QtWidgets.QPushButton(self.centralwidget)
        self.btn_close.setGeometry(QtCore.QRect(260, 180, 131, 101))
        self.btn_close.setObjectName("btn_close")
        self.btn_close.clicked.connect(self.on_close)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Win2"))
        self.btn_close.setText(_translate("MainWindow", "close"))

    def on_close(self):
        # win2 = QMainWindow()
        # ui = Ui_Win2()
        # ui.setupUi(win2)
        win2.close()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win2 = QMainWindow()
    ui = Ui_Win2()
    ui.setupUi(win2)
    win2.show()
    sys.exit(app.exec_())

像这样改变 win2:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

class Ui_Win2(object):
    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow
        MainWindow.setObjectName("Win2")
        MainWindow.resize(640, 480)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.btn_close = QtWidgets.QPushButton(self.centralwidget)
        self.btn_close.setGeometry(QtCore.QRect(260, 180, 131, 101))
        self.btn_close.setObjectName("btn_close")
        self.btn_close.clicked.connect(self.on_close)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Win2"))
        self.btn_close.setText(_translate("MainWindow", "close"))

    def on_close(self):
        # win2 = QMainWindow()
        # ui = Ui_Win2()
        # ui.setupUi(win2)
        self.MainWindow.close()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win2 = QMainWindow()
    ui = Ui_Win2()
    ui.setupUi(win2)
    win2.show()
    sys.exit(app.exec_())

self.MainWindow 而不是 self 调用关闭函数。并在 setupUi function

的第一行设置 self.MainWindow