Search code examples
pythonkivykivy-language

How can i use callback and update class variable


I am trying to update a class variable on interrupt callback from a physical switch

### .py

class Mode1(Screen):

    var= 0

    def my_callback(channel,self):
        self.var+=20

    gpio.add_event_detect(17, gpio.RAISING, callback=my_callback)

I need to update the variable when a physical button connected via raspberry pi is pressed. The Pressing of Button must be an interrupt/callback


Solution

  • When updating a class variable, do it inside a classmethod

    class Mode1(Screen):
        var = 1
        @classmethod
        def my_callback(cls, channel):
            cls.var += 20
    gpio.add_event_detect(17, gpio.RAISING, callback=Mode1.my_callback)