I am trying to create a Scatter kivy widget that scales freely, but as soon as the mouse button is up, it returns to the nearest zoom level.
It works, but it does not update the scale until the next click. I think I need to connect some kind of event here, but I'm pretty new to kiwi and can't understand. Here is my current code:
from kivy.app import App from kivy.uix.label import Label from kivy.uix.scatter import Scatter from kivy.graphics.transformation import Matrix ZOOM_LEVELS = [0.25, 0.5, 1, 2, 4] class PPMap(Scatter): def __init__(self, **kwargs): super(PPMap, self).__init__(**kwargs) self.bind(on_touch_up=self.adjust_zoom) def adjust_zoom(self, *args): old_zoom = self.scale new_zoom = min(ZOOM_LEVELS, key=lambda x:abs(x-old_zoom)) self.set_zoom(new_zoom) def set_zoom(self, zoom): self.transform.set(array=[[zoom, 0, 0, 0], [0, zoom, 0, 0], [0, 0, zoom, 0], self.transform.tolist()[3]]) class PPApp(App): def build(self): pp_map = PPMap(do_rotation=False, scale_min=ZOOM_LEVELS[0], scale_max=ZOOM_LEVELS[-1]) label = Label(text="Hello!", font_size=300, pos=(0, 0)) pp_map.add_widget(label) return pp_map if __name__ == "__main__": PPApp().run()
source share