0

i want to change object in all my subwindows this is my code

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

but it's always the last creating order subwindow that changes https://i.stack.imgur.com/DjZtf.png

how to change item in every subwindow? how to change text table in subwindow i want with over 10 subwindow?

Monarf
  • 19
  • 2

1 Answers1

0

Right now your label3 is stored in MainWindow, so when you cycle through your subwindows you just change the latest label. You can store it in each subwindow like this:

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()
LittleFoxyFox
  • 135
  • 1
  • 1
  • 11
  • thanks for the answer, but how to change the text for the active window or just multiple windows? – Monarf Nov 08 '20 at 12:51
  • see the edit. You can either change all labels through 'Change Text' or the current subwindow label with the added pushbutton – LittleFoxyFox Nov 08 '20 at 14:49