samedi 19 septembre 2020

Inverting boolean, when PyQt checkbox changes state

Basically I have a class object that has a couple of settings determined by the state of a set of True/False states and I would like to invert this True/False when a checkbox changes state in my PyQt GUI. Currently I do the following:

in my setup:

self.toggle_1.stateChanged.connect(lambda: self.change_state("setting_1"))
self.toggle_2.stateChanged.connect(lambda : self.change_state("setting_2"))
self.toggle_3.stateChanged.connect(lambda: self.change_state("setting_3"))

this then calls the function change_state

 def change_state(self, toggle_id):
    if toggle_id == "setting_1":
        self.anotherClass.setting_1 = not self.anotherClass.setting_1
    if toggle_id == "setting_2":
        self.anotherClass.setting_2 = not self.anotherClass.setting_2
    if toggle_id == "setting_3":
        self.anotherClass.setting_3 = not self.anotherClass.setting_3

and sure it works, but it would like to avoid the extra function and have something cleaner like

self.toggle_1.stateChanged.connect(self.anotherClass.setting_1 = not self.anotherClass.setting_1)

i.e define it in the setup, but the question is how? Haven't found anything googling and haven't had much success trying it myself...




Aucun commentaire:

Enregistrer un commentaire