PyQt appropriate function for 'Back' button for previous GUI frame

I use PyQt to develop a GUI. Like most GUIs it has a next and back button for next and previous frames. Every frame is defined in a seperate class. The thing is, for function of 'Next' button you can import next class of frame in the current class and then show() it. But for 'back', we recieve the error while trying import back the previous class. Can some one give any hint of logic to create respective function for 'Back' button.

Class for first frame:

from GUI2 import secondwindow

class firstwindow(object):
    def nextWindow(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = secondwindow()
        self.ui.setupUi(self.window)
        app.closeAllWindows()
        self.window.show()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(622, 471)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(210, 140, 191, 41))
        self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.nextWindow)
.....
.....
.....

second frame:

class secondwindow(object):
    def previousWindow(self):
        XXX
        XXX

    def setupUi(self, Dialog):

        Dialog.setObjectName("Dialog")
        Dialog.resize(1200, 650)
        Dialog.setMinimumSize(QtCore.QSize(552, 0))

        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(480, 240, 70, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.previouswindow)
...
...
...

1 answer

  • answered 2018-09-24 13:54 eyllanesc

    First of all the classes that Qt Designer offers are not widgets, and it is recommended that if you modify the .ui when recompiling you will lose the modifications of the logic. So for the 2 previous arguments I recommend you restore both files.

    ui_firstwindow.py

    class ui_Firstwindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(622, 471)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
    
            self.pushButton = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton.setGeometry(QtCore.QRect(210, 140, 191, 41))
            self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
            self.pushButton.setObjectName("pushButton")
            self.pushButton.clicked.connect(self.nextWindow)
    

    ui_secondwindow.py

    class ui_Secondwindow(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(1200, 650)
            Dialog.setMinimumSize(QtCore.QSize(552, 0))
    
            self.pushButton_2 = QtWidgets.QPushButton(Dialog)
            self.pushButton_2.setGeometry(QtCore.QRect(480, 240, 70, 31))
            self.pushButton_2.setObjectName("pushButton_2")
            self.pushButton_2.clicked.connect(self.previouswindow)
    

    Your problem is that to show a window you have to access the window object, but in your case if you want to do it in several files you may have problems with circular imports, undefined variables, etc. The correct thing is that all windows have the same scope.

    Then we will create a main.py file where we will implement the classes that implement the widgets using the previous design. We create a class where the windows will be created and we will connect the clicked signals to the show() method of the other window. In each class the clicked signal of the button is connected to the hide() method of the window.

    from PyQt5 import QtWidgets
    
    from ui_firstwindow import ui_Firstwindow
    from ui_secondwindow import ui_Secondwindow
    
    
    class Firstwindow(QtWidgets.QMainWindow, ui_Firstwindow):
        def __init__(self, parent=None):
            super(Firstwindow, self).__init__(parent)
            self.setupUi(self)
            self.pushButton.clicked.connect(self.hide)
    
    
    class Secondwindow(QtWidgets.QDialog, ui_Secondwindow):
        def __init__(self, parent=None):
            super(Secondwindow, self).__init__(parent)
            self.setupUi(self)
            self.pushButton_2.clicked.connect(self.hide)
    
    
    class Manager:
        def __init__(self):
            self.first = Firstwindow()
            self.second = Secondwindow()
    
            self.first.pushButton.clicked.connect(self.second.show)
            self.second.pushButton_2.clicked.connect(self.first.show)
    
            self.first.show()
    
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        manager = Manager()
        sys.exit(app.exec_())