简体   繁体   中英

Switching between windows PyQt designer

I have been trying to develop a software in PyQt5.

I have 3 windows: 1) login 2) dashboard 3) appointments

Now I can navigate properly from login -> dashboard -> appointments perfectly. But when I press pushbutton for back from appointments, it doesn't come back to dashboard.

But if I go from dashboard->appointments, the pushbutton works for 1 time.

I have removed irrelevant lines from the code to make analysis simpler.

login window class:

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow

    def retranslateUi(self, MainWindow):
        self.pushButton.clicked.connect(self.next)
 
    def next(self):
        ui = Ui_MainWindow2()
        ui.setupUi(self.MainWindow)
        self.MainWindow.show()

dashboard window class:

class Ui_MainWindow2(object):

    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow

    def retranslateUi(self, MainWindow):
        self.pushButton.clicked.connect(self.next)
 
    def next(self):
        ui = Ui_MainWindow3()
        ui.setupUi(self.MainWindow)
        self.MainWindow.show()

appointment window class:

class Ui_MainWindow3(object):

    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow

    def retranslateUi(self, MainWindow):
        self.pushButton.clicked.connect(self.back)
 
    def back(self):
        ui = Ui_MainWindow2()
        ui.setupUi(self.MainWindow)
        self.MainWindow.show()

Application starts from file login.py which has:

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

The forward navigation always work, the backward one keeps failing...

Any help or suggestions will be very welcome...

Turns out I was writing the next or back function slightly differently than it should have been. The next and back functions should have been like this:

def next(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow2()
        self.ui.setupUi(self.window)
        self.oldWindow.hide()
        self.window.show()

where self.oldWindow is the reference to the original window of the class enclosing the function next. This is also same for the back function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM