Skip to content
Sunip Mukherjee edited this page Oct 4, 2022 · 3 revisions

Links

  1. PyQt use built-in icons
  2. Plot mouse hover callback

Example for value class with update callback

class SObject:
    def __init__(self, initial_value: int | float):
        self._value = initial_value
        self._callbacks = []
    
    def set_callback(self, fn):
        try:
            fn(self._value)
        except Exception as e:
            raise e
        self._callbacks.append(fn)
    @property
    def value(self):
        return self._value
    
    @value.setter
    def value(self, val):
        self._value = val
        for fn in self._callbacks:
            fn(self._value)

def print_on_set(val):
    print('Value set to:', val)

if __name__ == '__main__':
    var = SObject(0) # create an SObject with 0 initial value
    var.set_callback(print_on_set) # set callback that is executed when value of var is set, should execute print_on_set to make sure it works
    var.value = 10 # should execute print_on_set, and print 10
    print('Value in main:', var.value)
Clone this wiki locally