如何使用鼠标滚轮使用 Kivy 缩放矩形?

How to scale rectangle with Kivy using mousewheel?

使用 python 和 kivy,我想平移(用鼠标拖动)并使用鼠标滚轮缩放 up/down 在散点小部件 canvas 中绘制的矩形。

我正在使用 ubuntu 和 kivy 1.11。

from kivy.config import Config
Config.set('input', 'mouse', 'mouse,disable_multitouch,disable_on_activity')

from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.graphics import (Color, Rectangle)
from kivy.graphics.transformation import Matrix


class World(Scatter):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.do_rotation = False
        self.scale_min = .1
        self.scale_max = 10
        self.top = 500

        with self.canvas.after:
            Color(0, 1, 0)
            self.rect = Rectangle(pos=self.pos, size=self.bbox[1])

        self.bind(pos=self._update_rect, size=self._update_rect)

    def _update_rect(self, instance, _):
        self.rect.pos = self.pos
        self.rect.size = self.bbox[1]

    def on_touch_move(self, touch):
        print("on_touch_move:", touch.profile, touch)
        return super().on_touch_move(touch)

    def on_touch_down(self, touch):
        print("on_touch_down:", touch.profile, touch)
        return super().on_touch_down(touch)

    def on_touch_up(self, touch):
        if self.collide_point(*touch.pos):
            print("on_touch_up:", touch.profile, touch)

            if touch.is_mouse_scrolling:
                print ("is_mouse_scrolling")
                if touch.button == 'scrolldown':
                    print("scrolldown")
                    mat = Matrix().scale(.9, .9, .9)
                    self.apply_transform(mat, anchor=touch.pos)
                    return True
                elif touch.button == 'scrollup':
                    print("scrollup")
                    mat = Matrix().scale(1.1, 1.1, 1.1)
                    self.apply_transform(mat, anchor=touch.pos)
                    return True

        return super().on_touch_up(touch)

class ScatterApp(App):
    def build(self):
        return World(size=(400, 400), size_hint=(None, None))


if __name__ == '__main__':
    ScatterApp().run()

到目前为止,我可以用鼠标平移矩形。然后我可以用鼠标滚轮缩放 up/down 矩形。但是后来我无法再平移矩形了。

似乎使用鼠标滚轮重新启用 multitouch_sim? 请注意,一开始我禁用 multitouch_sim:

Config.set('input', 'mouse', 'mouse,disable_multitouch,disable_on_activity')

通常,每当您实现覆盖超级 class 方法的事件处理方法时,您应该使用 super() 调用覆盖的方法。当你在不调用 super 的情况下执行 return 时,你可能会短路基数 class 正在做的事情。在某些情况下,您可能想要替换基础 class 处理,但您需要研究基础 class(es) 方法以确定这是否可行。因此,在您的特定情况下,我认为您只需要从 on_touch_up() 方法中删除两个 return True 语句,以允许 Scatter 基本 Widget 完成其触摸处理。