Question

Running a code posted below brings a Dialog_01 QMainWindow with one button on it. Clicking the button should close this window (Dialog_01) and replace it with another: Dialog_02 which is practically a copy of the first. Clicking Dialog_02 button closes it and reopens Dialog_01. There are no errors while the code runs. But it is definitely missing something since Dialog_02 is not showing up even while it is being declared. What am I missing?

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("Press to close this dialog 02 and re-open Dialog 01")
        Button_02.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_02)

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

    def callAnotherQMainWindow(self):
        print "This is supposed to call Dialog_01"



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

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

        Button_01 = QtGui.QPushButton("Press to close this dialog and open Dialog 02")
        Button_01.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_01)        

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

    def callAnotherQMainWindow(self):
        dialog_1.hide()

        dialog_2 = Dialog_02()
        dialog_2.resize(480,320)
        dialog_2.show()
        dialog_2.raise_() 


dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())

EDITED LATER: WORKING FIXED VERSION

The mistake was in using dialog.hide() instead of dialog.close(). A calling dialog needs to be closed (instead of hidden) before a second dialog can be displayed.

from PyQt4 import QtCore, QtGui    
app = QtGui.QApplication(sys.argv)

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

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

        Button_02 = QtGui.QPushButton("Press to close this dialog 02 and re-open Dialog 01")
        Button_02.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_02)

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

    def callAnotherQMainWindow(self):
        self.close()
        self.dialog_01 = Dialog_01()
        self.dialog_01.show()
        self.dialog_01.raise_()



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

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

        Button_01 = QtGui.QPushButton("Press to close this dialog 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.close()
        self.dialog_02 = Dialog_02()
        self.dialog_02.show()
        self.dialog_02.raise_()

dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
Was it helpful?

Solution

in Dialog_02 class modify the following method:

def callAnotherQMainWindow(self)
    self.close()
    self.dialog_01 = Dialog_01()
    self.dialog_01.show()
    self.dialog_01.raise_()

And in Dialog_01 class modify the following method:

def callAnotherQMainWindow(self)
    self.close()
    self.dialog_02 = Dialog_02()
    self.dialog_02.show()
    self.dialog_02.raise_()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top