Modified kivy scatter widget does not update conversion

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() 
+5
source share
2 answers
 self.transform.set( # ... 

The problem is that you are modifying an existing ObjectProperty instance, and Kivy does not know that it has changed:

A warning. To mark a property as changed, you must reassign the new python object.

You can do this or, for example, manually send:

 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]]) self.property('transform').dispatch(self) # transform has changed 
+1
source

Take a look at this method https://kivy.org/docs/api-kivy.uix.scatter.html#kivy.uix.scatter.Scatter.apply_transform

You can rewrite set_zoom similarly

 def set_zoom(self, new_zoom, old_zoom): zoom = new_zoom / old_zoom self.apply_transform(Matrix().scale(zoom, zoom, zoom)) 

if you divide by old_zoom and multiply with what you want, you should get the right zoom because it seems to be multiplicative.

Here is another useful link where they work with Scatter and optimize it for the desktop.

+1
source

Source: https://habr.com/ru/post/1272573/


All Articles