There are 20 checkboxes in the user's window, in the code their name is checkbox_1, checkbox_2, ... checkbox_20. To activate all the checkboxes at the same time, I use this algorithm:
def select_all_W(self):
self.Ui.checkBox_1.setChecked(True)
self.Ui.checkBox_2.setChecked(True)
# .................................
self.Ui.checkBox_20.setChecked(True)
This makes the code very large and heavy. Is it possible to do the same thing through some function or, for example, through a loop:
for i in range 20
self.Ui.checkBox_[i].setChecked(True)
In other words, how can you simplify the activation of a large number of window elements? At the bottom is a part of the program code where the enumeration of checkbox values is implemented.
class Win_menu(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.Ui = Ui_MainWindow()
self.Ui.setupUi(self)
self.show()
self.Ui.pushButton_3.clicked.connect(self.select_all_W)
self.Ui.pushButton_4.clicked.connect(self.select_all_W)
self.Ui.pushButton_5.clicked.connect(self.select_all_A)
self.Ui.pushButton_6.clicked.connect(self.select_all_A)
def win_dlg(self):
self.setup_games()
self.Win_menu4 = Win_menu4()
self.Win_menu4.show()
def setup_games(self):
global warriors, magics, scouts, healers, special_W, special_M, special_S, special_H, leutents, cardOL, base_cardOl
if self.Ui.checkBox_1.isChecked():
warriors.extend(hero_B_W)
magics.extend(hero_B_M)
....
if self.Ui.checkBox_9.isChecked():
warriors.append('Karnon')
if self.Ui.checkBox_10.isChecked():
warriors.append('korbin')
if self.Ui.checkBox_11.isChecked():
warriors.append('Kruzbek')
def select_all_A(self):
self.Ui.checkBox_49.setChecked(True)
self.Ui.checkBox_50.setChecked(True)
self.Ui.checkBox_51.setChecked(True)
def reset_all_A(self):
self.Ui.checkBox_49.setChecked(False)
self.Ui.checkBox_50.setChecked(False)
self.Ui.checkBox_51.setChecked(False)
hero_B_W = ['Grisban', 'Sindrael']
hero_B_M = ['Tarcha', 'Leorik']
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Win_menu()
window.show()
sys.exit(app.exec_())
xorself.something) are not dynamic and there's no semantic syntax to allow that directly in the name. Not onlyself.Ui.checkBox_[i]wouldn't allow you to access objects with names starting with "checkBox_", but it also has a very different meaning, as square brackets are used for item access: the attempt above would be valid only ifcheckBox_was an item based object (like a list). If the objects are referenced as attributes, usegetattr():getattr(self.Ui, f'checkBox_{i}').