dimanche 10 juin 2018

one checkbox that uncheck all other checkboxes in PyQt5 / PySide2

I have a series of checkboxes (P1, P2, P3) an I want that the selected checkbox uncheck the others. The code below works, but as I have in the application a total of 12 checkboxes, it takes around 200 lines of codes. Simplifying the code would reduce the number of lines (1075 in total) and hopefully reduce the time that the application is taking to launch (presently 35 sec)

self.checkbox_P1= QtWidgets.QCheckBox("P1",self)
self.checkbox_P1.clicked.connect(self.P_uncheck_others)
self.checkbox_P2= QtWidgets.QCheckBox("P2",self)
self.checkbox_P2.clicked.connect(self.P_uncheck_others)
self.checkbox_P3= QtWidgets.QCheckBox("P3",self)
self.checkbox_P3.clicked.connect(self.P_uncheck_others)

def P_uncheck_others(self):

    sender = self.sender()

    if sender.text() == "P1":
            # self.checkbox_P1.setChecked(False)
            self.checkbox_P2.setChecked(False)
            self.checkbox_P3.setChecked(False)
    elif sender.text() == "P2":
            self.checkbox_P1.setChecked(False)
            # self.checkbox_P2.setChecked(False)
            self.checkbox_P3.setChecked(False)
    elif sender.text() == "P2":
            self.checkbox_P1.setChecked(False)
            self.checkbox_P2.setChecked(False)
            # self.checkbox_P3.setChecked(False)




Aucun commentaire:

Enregistrer un commentaire