Pyqt 中下一页和上一页的问题

发布于 2025-01-10 17:15:23 字数 1820 浏览 0 评论 0原文

所以我目前正在做一个小项目,但我遇到了一个问题,我尝试过的所有方法都不起作用。我有 2 个文件: 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_())

和 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()

我运行 Page1test 的代码:只有一个 Qpushbutton“下一页”的空窗口,目标:我们单击它,它打开第 2 页(并关闭第 1 页)。而且,当我们在第 2 页时,我们有一个带有“上一页”的 Qpushbutton,当我们单击它时,它会打开第 1 页,并关闭第 2 页。就像一个循环。 但是,当我运行代码时,它返回一个错误: 无法从部分初始化的模块“page2test”导入名称“Page2”(很可能是由于循环导入)

,我不知道如何修复它... 如果有人有一个想法,那将会非常有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2025-01-17 17:15:23

所以,我终于找到了解决方案,最终并不是那么困难。就是这样(如果它可以帮助某人):

我没有制作 2 个文件,而是只制作了 1 个文件。这是代码:

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_())
```

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_())
```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文