我如何从PyQt中的不同窗口更新窗口小部件?

问题描述 投票:1回答:1

如何从窗口B的窗口A更新小部件?我有一个要在第二个窗口中更改的小部件,如何从这里继续?

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        #MyWidget
        ...

    def openSecond(self):
        self.SW = SecondWindow(#MyWidget)
        self.SW.show()


class SecondWindow(QMainWindow):
    def __init__(self, #MyWidget):
        super(SecondWindow, self).__init__()
        #MyWidget.changed
        #Update to parent
        ...
python python-3.x pyside2
1个回答
0
投票

[在创建类B的实例时将A类的自身作为参数传递,然后您可以使用self.parent访问B类中A类的项。

from PySide2.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.button = ''
        btn = QPushButton('Open Second', self)
        btn.move(10, 10)
        btn.clicked.connect(self.openSecond)

        self.resize(420, 450)

        button = QPushButton(self)
        button.move(100,100)
        button.setText("current text")
        self.button = button

    def openSecond(self):
        self.SW = SecondWindow(self.button, parent=self)
        self.SW.show()


class SecondWindow(QMainWindow):
    def __init__(self, button, parent=None):
        super(SecondWindow, self).__init__()
        self.parent = parent
        lbl = QLabel('Second Window', self)
        button.setText("updated text")
        self.parent.buttons = button


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    MW = MainWindow()
    MW.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.