Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

python - PyQt: How to hide QMainWindow

Clicking Dialog_01's button hides its window and opens Dialog_02. Clicking Dialog_02's button should close its windows and unhide Dialog_01. How to achieve it?

import sys, os
from PyQt4 import QtCore, QtGui    

class Dialog_02(QtGui.QMainWindow):
    def __init__(self):
        super(Dialog_02, self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       

        Button_02 = QtGui.QPushButton("Close THIS and Unhide Dialog 01")
        Button_02.clicked.connect(self.closeAndReturn)
        myBoxLayout.addWidget(Button_02)

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 02')

    def closeAndReturn(self):
        self.close()

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(Dialog_01, self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       

        Button_01 = QtGui.QPushButton("Hide THIS and Open Dialog 02")
        Button_01.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_01)        

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 01')

    def callAnotherQMainWindow(self):
        self.hide()
        self.dialog_02 = Dialog_02()
        self.dialog_02.show()
        self.dialog_02.raise_()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    sys.exit(app.exec_())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make the first window a parent of the second window:

class Dialog_02(QtGui.QMainWindow):
    def __init__(self, parent):
        super(Dialog_02, self).__init__(parent)
        # ensure this window gets garbage-collected when closed
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
    ...    

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

class Dialog_01(QtGui.QMainWindow):
    ...

    def callAnotherQMainWindow(self):
        self.hide()
        self.dialog_02 = Dialog_02(self)
        self.dialog_02.show()

If you want the same dialog to be shown each time, do something like:

    def callAnotherQMainWindow(self):
        self.hide()
        if not hassattr(self, 'dialog_02'):
            self.dialog_02 = Dialog_02(self)
        self.dialog_02.show()

and hide() the child window, rather than closing it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...