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.sstatic.net/DjZtf.png
how to change item in every subwindow? how to change text table in subwindow i want with over 10 subwindow?
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()