0

我想更改我所有子窗口中的对象这是我的代码

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets

class MainWindow(QtWidgets.QMainWindow):
    count = 0
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.mdi = QMdiArea()
        self.setCentralWidget(self.mdi)
        bar = self.menuBar()

        file = bar.addMenu("Subwindow")
        file.addAction("New")
        file.addAction("Change Text")
        file.triggered[QAction].connect(self.click)
        self.setWindowTitle("Multiple window using MDI")

    def click(self,action):
        print("New sub window")
        if action.text() == "New":
            MainWindow.count = MainWindow.count + 1
            sub = QMdiSubWindow()
            sub.setWidget(QTextEdit())
            sub.setWindowTitle("subwindow" + str(MainWindow.count))
            self.subwindow = self.mdi.addSubWindow(sub)
            self.subwindow.show()
            self.label3 = QtWidgets.QLabel(sub)
            self.label3.setGeometry(10, 80, 500, 10)
            self.label3.setText('Default')
            self.label3.show()
        if action.text() == "Change Text":
            for i in self.mdi.subWindowList():
                label1 = QtWidgets.QLabel(i)
                label1.setGeometry(10,50,500,10)
                label1.setText(str(i))
                label1.show()
                self.label3.setText('TRUE')
                print(i)

def main():
    app = QApplication(sys.argv)
    ex = MainWindow()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

但它始终是更改https://i.stack.imgur.com/DjZtf.png的最后一个创建订单子窗口

如何更改每个子窗口中的项目?如何使用超过 10 个子窗口更改我想要的子窗口中的文本表?

4

1 回答 1

0

现在您label3的存储在 中MainWindow,因此当您循环浏览子窗口时,您只需更改最新标签。您可以将其存储在每个子窗口中,如下所示:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    count = 0

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.mdi = QMdiArea()
        self.setCentralWidget(self.mdi)
        bar = self.menuBar()

        file = bar.addMenu("Subwindow")
        file.addAction("New")
        file.addAction("Change Text")
        file.triggered[QAction].connect(self.click)
        self.setWindowTitle("Multiple window using MDI")

    def click(self, action):
        print("New sub window")
        if action.text() == "New":
            MainWindow.count = MainWindow.count + 1
            sub = QMdiSubWindow()
            sub.setWidget(QTextEdit())
            sub.setWindowTitle("subwindow" + str(MainWindow.count))
            self.subwindow = self.mdi.addSubWindow(sub)
            self.subwindow.show()
            # change current subwindow label text
            button = QPushButton("Click to change", sub)
            button.clicked.connect(lambda: sub.label3.setText('TRUE'))

            sub.label3 = QtWidgets.QLabel(sub)
            sub.label3.setGeometry(10, 80, 500, 10)
            sub.label3.setText('Default')

            sub_layout = self.subwindow.layout()
            sub_layout.addWidget(sub.label3)
            sub_layout.addWidget(button)

        if action.text() == "Change Text":
            for i in self.mdi.subWindowList():
                label1 = QtWidgets.QLabel(i)
                label1.setGeometry(10, 50, 500, 10)
                label1.setText(str(i))
                label1.show()
                i.label3.setText('TRUE')
                print(i)


def main():
    app = QApplication(sys.argv)
    ex = MainWindow()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
于 2020-11-08T11:08:33.083 回答