使用QStackedWidget换屏时,如何调用换屏中的函数?

问题描述

我正在尝试调用屏幕的 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",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 函数,以便我可以在其中运行我想要的任何代码

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

解决方法

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

我强烈建议您阅读documentation about classes in 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 主窗口旨在用作顶级窗口;另请注意,使用固定几何图形(位置和大小)通常被认为是不好的做法,您应该改用 layout managers