mercredi 28 octobre 2020

Is there a way to "freeze" the state of a checkbox in PyQt5?

I want a checkbox being automatically checked when I check another checkbox and not being uncheckable until I uncheck the other checkbox. I thought "freezing" would be an easy way to do this but I'm also interested in other solutions. An example program try this out would be:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QCheckBox

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(500, 50, 500, 500)
        self.box1 = QCheckBox('Box1', self)
        self.box1.setGeometry(250, 10, 50, 50)
        self.box1.stateChanged.connect(self.dothething)
        self.box2 = QCheckBox('Box2', self)
        self.box2.setGeometry(10, 10, 50, 50)
        self.show()
    
    def dothething(self):
        if not self.box2.isChecked():
            self.box2.toggle()
            self.box2.freeze()
        else: 
            self.box2.unfreeze()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Window()
    sys.exit(app.exec_())

Obviously the .freeze() and .unfreeze() functions don't exist.

I also tried .setCheckable but with this the CheckBox can only be frozen in an unchecked state so that's the opposite of what I want.




Aucun commentaire:

Enregistrer un commentaire