从python gui打开新窗口

时间:2012-11-02 16:32:09

标签: python user-interface window pyqt

我在python gui app中打开新窗口时遇到问题。我有3个班级(首先登录,然后打开2个窗口)。这很好用:

class LoginDialog(QtGui.QDialog):
    def __init__(self, parent = None):
        super(LoginDialog, self).__init__(parent)
    .....

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
            QtGui.QWidget.__init__(self, parent)
    .....

class ImageViewerMainWindow(QtGui.QMainWindow):
    def __init__(self, path, parent = None):
        super(ImageViewerMainWindow, self).__init__(parent)
    .....

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

    loginDlg = LoginDialog()
    if not loginDlg.exec_():
        sys.exit(-1)

    MyMainWindow = MainWindow()
    MyMainWindow.show()

    viewer = ImageViewerMainWindow("C:\image.jpg")
    viewer.show()

    sys.exit(qtApp.exec_())

我需要从MainWindow执行查看器,但是当我这样说它只是闪烁并消失:

class MainWindow(QtGui.QMainWindow):
        def __init__(self, parent = None):
                QtGui.QWidget.__init__(self, parent)
        .....
        def DoOpenImageViewer(self):
                viewer = ImageViewerMainWindow("C:\image.jpg")
                viewer.show()

1 个答案:

答案 0 :(得分:1)

您需要保留对查看者的引用,否则当viewer超出范围并被垃圾收集时,新窗口将被销毁。 如果您一次只需要一个窗口,则可以执行以下操作:

class MainWindow(QtGui.QMainWindow):
        def __init__(self, parent = None):
                QtGui.QWidget.__init__(self, parent)
        .....
        def DoOpenImageViewer(self):
                self.viewer = ImageViewerMainWindow("C:\image.jpg")
                self.viewer.show()

否则,您可以使用列表来存储引用。

相关问题