-2

我正在尝试调用屏幕的 init 函数,我将屏幕索引更改为

例如,我有以下代码:

from PyQt5 import QtWidgets   as qtw
from PyQt5 import QtGui       as qtg
from sys   import argv        as sysArgv
from sys   import exit        as sysExit

arialLarge = qtg.QFont("Arial", 18)

class MainWindow(qtw.QWidget):
    def __init__(self):
        super().__init__()
        # Current screen label;
        mainWindowLabel = qtw.QLabel("This is the main window", self)
        mainWindowLabel.setFont(arialLarge)
        mainWindowLabel.move(20, 40)
        # Button for going to the HelloWindow screen;
        gotoHelloWindowButton = qtw.QPushButton("Go to hello window", self, clicked=lambda: appStack.setCurrentIndex(appStack.currentIndex()+1))
        gotoHelloWindowButton.move(100, 100)


class HelloWindow(qtw.QWidget):
    def __init__(self):
        super().__init__()
        # EG: print hello world when I visit this page
        print("hello world")


        # Current screen label;
        helloWindowLabel = qtw.QLabel("This is the hello window", self)
        helloWindowLabel.setFont(arialLarge)
        helloWindowLabel.move(20, 40)
        # Button for going to the MainWindow screen;
        gotoMainWindowButton = qtw.QPushButton("Go to main window", self, clicked=lambda: appStack.setCurrentIndex(appStack.currentIndex()-1))
        gotoMainWindowButton.move(100, 100)


if __name__ == "__main__":
    app = qtw.QApplication(sysArgv)
    appStack = qtw.QStackedWidget()
    appStack.addWidget(MainWindow())
    appStack.setFixedSize(300, 300)
    appStack.show()
    appStack.addWidget(HelloWindow())
    sysExit(app.exec())

如果我从 MainWindow 访问 HelloWindow,我如何运行 HelloWindow 屏幕的 init 函数,以便我可以在那里运行我想要的任何代码?

我需要能够在我正在处理的应用程序上执行此操作,因为在主页上我已经动态创建了按钮,这些按钮都具有对我的服务器具有不同索引的函数参数,并且我需要能够从服务器获取数据单击按钮的数据索引,因此在其他页面上我可以查看所需的数据。

4

1 回答 1

0

python 类的__init__是在创建实例时调用的(使用SomeClass()),因此您不应尝试(甚至认为)再次调用它,因为它可能会产生难以跟踪的严重问题和错误。

我强烈建议您阅读有关 Python 中的类的文档,因为您不能忽略面向对象编程中的这一方面。

如果您需要在每次更改索引时调用某些内容,那么您应该更好地继承 QStackedWidget 并从那里控制所有内容。

一个好的解决方案是创建一个标准化的函数,该函数将在每次呈现页面时调用,并确保堆栈小部件正确调用它。

class FirstPage(QtWidgets.QWidget):
    def __init__(self):
        super().__init__(self)
        # ...
        self.nextButton = QtWidgets.QPushButton('Next')
        self.doSomething()

    def doSomething(self):
        ...


class SecondPage(QtWidgets.QWidget):
    def __init__(self):
        super().__init__(self)
        # ...
        self.prevButton = QtWidgets.QPushButton('Previous')
        self.doSomething()

    def doSomething(self):
        ...


class Stack(QtWidgets.QStackedWidget):
    def __init__(self):
        super().__init__(self)
        self.first = FirstPage()
        self.first.nextButton.clicked.connect(self.goNext)
        self.addWidget(self.first)

        self.second = SecondPage()
        self.second.prevButton.clicked.connect(self.goPrev)

        self.currentChanged.connect(self.initCurrent)

    def goNext(self):
        self.setCurrentIndex(1)

    def goPrev(self):
        self.setCurrentIndex(0)

    def initCurrent()
        if self.currentWidget():
            self.currentWidget().doSomething()


if __name__ == "__main__":
    app = qtw.QApplication(sysArgv)
    appStack = Stack()
    appStack.setFixedSize(300, 300)
    appStack.show()
    sysExit(app.exec())

请注意,将 QMainWindow 添加到父级不是一个好主意,因为 Qt 主窗口旨在用作顶级窗口;另请注意,使用固定几何(位置和大小)通常被认为是不好的做法,您应该改用布局管理器

于 2021-04-11T19:52:02.523 回答