Search code examples
pyqtpyqt5windowqwidget

Issue in Pyqt with next and previous Pages


so I'm currently working on a little project but I have an issue and all what I've tried did not work. I have 2 files : Page1test :

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QPushButton, QWidget, QLabel
import sys

from page2test import Page2

class Page1(QWidget):
    def __init__(self):
        super(Page1, self).__init__()
        self.setWindowTitle("Page 1")

        label1 = QLabel(self)
        label1.setText("\n PAGE 1")

        self.btn_inMyApp = QPushButton ('Next page', self)
        self.btn_inMyApp.setGeometry(1500,800,275,125)
        self.btn_inMyApp.clicked.connect(self.closePage1_OpenPage2)
        self.show()

    def closePage1_OpenPage2(self):
        self.Open = Page2()
        self.Open.showMaximized()
        self.close()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Page1()
    window.showMaximized()
    sys.exit(app.exec_())

And page2test :


from PyQt5.QtWidgets import QPushButton, QWidget, QLabel

from Page1test import Page1

class Page2(QWidget):
    def __init__(self):
        super(Page2, self).__init__()
        self.setWindowTitle("Test window principale")

        label1 = QLabel(self)
        label1.setText("\n Page2")

        self.btn_inMyApp = QPushButton ('previous Page', self)
        self.btn_inMyApp.setGeometry(1500,800,275,125)
        self.btn_inMyApp.clicked.connect(self.closePage2_OpenPage1)
        self.show()

    def closePage2_OpenPage1(self):
        self.Open = Page1()
        self.Open.showMaximized()
        self.close()

I run the code of Page1test : empty window with just a Qpushbutton "Next Page", goal : we click on it and it open Page 2 (and close Page 1). And, when we are in Page 2, we have A Qpushbutton with "Previous Page" and when we click on it, it opens page 1, and close Page 2. Like a loop. But, when I run the code, it returns an error : cannot import name 'Page2' from partially initialized module 'page2test' (most likely due to a circular import)

and I have no idea how to fix it... If someone had an idea, it would be really helpful.


Solution

  • So, I've finally found the solution, that was not so difficult in the end. Here it is (if it can help someone) :

    Instead of making 2 files, I've done just 1 file. Here is the code :

    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import QPushButton, QWidget, QLabel, QMainWindow
    import sys
    
    
    class MyApp(QWidget):
        def __init__(self):
            super(MyApp, self).__init__()
            self.setWindowTitle("Page 1")
    
            label1 = QLabel(self)
            label1.setText("\n PAGE 1")
    
            self.btn_inMyApp = QPushButton ('Page suivante', self)
            self.btn_inMyApp.setGeometry(1500,800,275,125)
            self.btn_inMyApp.clicked.connect(self.closePage1_OpenPage2)
            self.show()
    
        def btn1_onClicked(self):
            pass
    
        def closePage1_OpenPage2(self):
            self.Open = NewApp()
            self.Open.showMaximized()
            self.close()
    
    
    class NewApp(QMainWindow):
        def __init__(self):
            super(NewApp, self).__init__()
            self.setWindowTitle("Test window principale")
    
            label1 = QLabel(self)
            label1.setText("\n Page2")
    
            self.btn_inMyApp = QPushButton ('previous Page', self)
            self.btn_inMyApp.setGeometry(1500,800,275,125)
            self.btn_inMyApp.clicked.connect(self.closePage2_OpenPage1)
            self.show()
    
        def closePage2_OpenPage1(self):
            self.Open = MyApp()
            self.Open.showMaximized()
            self.close()
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = MyApp()
        window.showMaximized()
        sys.exit(app.exec_())
    ```