python - 如何确定第二个窗口是否关闭

标签 python pyqt5

我有一个应用程序中途使用了其他人的另一个应用程序(两个应用程序都使用 .ui 文件)。因此,我在 SecondWindow 中创建第二个应用程序并隐藏 MainWindow。现在我想在 SecondWindow 关闭后再次显示 MainWindow 。我发现the solution in the answer有效,但现在 SecondWindow 的背景是错误的,因为它使用了 MainWindow 的背景。有没有办法找出 SecondWindow 是否在 MainWindow 的类中关闭,而不使 MainWindow 成为 SecondWindow 的父级> 还是为了防止因 parent 身份造成的背景变化?

我当前的代码看起来有点像这样:

## Define main window class from template
path = os.path.dirname(os.path.abspath(__file__))
uiFile = os.path.join(path, 'test.ui')
Ui_MainWindow, QtBaseClass = uic.loadUiType(uiFile)

uiFile2 = os.path.join(path, 'monitor.ui')
WindowTemplate, SecondWindowClass = pg.Qt.loadUiType(uiFile2)

class SecondWindow(SecondWindowClass):

    def closeThis(self):
        self.close()
        self.parent().show()

    def __init__(self, parent):
        super(SecondWindow, self).__init__(parent)
        # ensure this window gets garbage-collected when closed
        self.setWindowTitle('pyqtgraph example: Qt Designer')
        self.ui = WindowTemplate()
        self.ui.setupUi(self)
        self.show()

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):

     def showSecond(self):
        self.second.show()
        self.hide()

     def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.ui=uic.loadUi(uiFile, self)
        self.setupUi(self)
        self.show()
        self.second = SecondWindow(self)
        self.second.hide()
        self.ui.end_button.clicked.connect(lambda x:self.showSecond())

win = MainWindow()
if __name__ == '__main__':
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
       QtGui.QApplication.instance().exec_()

最佳答案

实际上第二个窗口不必是第一个窗口的子窗口。

所以你应该能够做这样的事情:

class SecondWindow(SecondWindowClass):    
    def closeThis(self):
        self.close()
        self.first.show()

    def __init__(self, first):
        super(SecondWindow, self).__init__()
        self.first = first
        ...

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
     def showSecond(self):
        self.second.show()
        self.hide()

     def __init__(self):
        ...
        self.second = SecondWindow(self)
        self.second.hide()

关于python - 如何确定第二个窗口是否关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46407062/

相关文章:

python - 在循环中切片 NumPy 数组

python - 使用递归函数将嵌套列表转换为集合

python - 属性错误 : 'file' object has no attribute 'DictReader'

python - get_jwt_identity() 在 Flask-JWT-Extended 中返回 None

python - 将导入的函数连接到 Qt5 进度条,无需依赖

python - QTreeWidget 中的拖放操作不复制拖放的项目

python - python pyqt5 类引用

python - 如何将 tensorflow-hub 模块与 tensorflow-dataset api 一起使用

python - 如何将鼠标点击信号连接到 pyqtgraph 绘图小部件

python - 将 QPixmap 保存到 PostgreSQL 而不使用准备好的查询