Skip to main content
 首页 » 编程设计

python之创建以与 on_touch_down 相同的方式分派(dispatch)事件冒泡的 kivy 行为

2024年10月17日8softidea

让我们从基本示例开始:

class OuterWidget(Widget): 
    def on_touch_down(self, touch): 
        if self.collide_point(*touch.pos): 
            print('on_touch_down', self) 
            return True  # doesn't dispatch to InnerWidget 
        return super().on_touch_down(touch) 
 
 
class InnerWidget(Widget): 
    def on_touch_down(self, touch): 
        if self.collide_point(*touch.pos): 
            print('on_touch_down', self) 
        return super().on_touch_down(touch) 

事件首先派发给 OuterWidget,如果它调用 super().on_touch_down(touch) 事件派发给子部件,否则接下来不会派发事件。这很清楚。

我想创建一些发生在 on_touch_down 上的行为,并且以相同的方式行事。让我们试试:

class TestBehavior(Widget): 
    def __init__(self, **kwargs): 
        self.register_event_type('on_test_event') 
        super().__init__(**kwargs) 
 
    def on_test_event(self, touch): 
        pass 
 
    def on_touch_down(self, touch): 
        self.dispatch('on_test_event', touch)  # Some event that happens with on_touch_down 
        return super().on_touch_down(touch) 
 
 
class OuterWidget(TestBehavior, Widget): 
    def on_test_event(self, touch): 
        if self.collide_point(*touch.pos): 
            print('on_test_event', self) 
            return True  # doesn't affect anything, both widgets would recieve on_test_event 
        return super().on_test_event(touch) 
 
 
class InnerWidget(TestBehavior, Widget): 
    def on_test_event(self, touch): 
        if self.collide_point(*touch.pos): 
            print('on_test_event', self) 
            return True 
        return super().on_test_event(touch) 

on_test_event 冒泡的工作方式与 on_touch_down 不同。无论 OuterWidget 返回什么,事件都会被分派(dispatch)到 InnerWidget。发生这种情况是因为两个小部件都会收到触发 on_test_event 的 on_touch_down 事件。

看起来如果我使用 on_touch_down 来分派(dispatch)行为中的某个事件,则此事件将始终分派(dispatch)给所有子级,无论他们返回什么。但是,如果某些小部件没有调用 super().on_test_event(touch),我接下来不想调度 on_test_event。

我该怎么做?有什么想法吗?

请您参考如下方法:

Widget 类像这样处理所有子类的事件:

(复制自 widget.py)

def on_touch_down(self, touch): 
    '''Receive a touch down event. 
    :Parameters: 
        `touch`: :class:`~kivy.input.motionevent.MotionEvent` class 
            Touch received. The touch is in parent coordinates. See 
            :mod:`~kivy.uix.relativelayout` for a discussion on 
            coordinate systems. 
    :Returns: bool 
        If True, the dispatching of the touch event will stop. 
        If False, the event will continue to be dispatched to the rest 
        of the widget tree. 
    ''' 
    if self.disabled and self.collide_point(*touch.pos): 
        return True 
    for child in self.children[:]: 
        if child.dispatch('on_touch_down', touch): 
            return True 

如您所见,它迭代其子项并分派(dispatch)事件,直到返回 True。

要创建类似的行为,您可以创建自己的混合类并使所有类都继承自它或**monkey patch Widget **

恕我直言,这会有点乱,我要做的是创建一个函数:

def fire_my_event(widget): 
    if hasattr(widget, 'on_test_event'): 
       if widget.on_test_event(): 
          return True 
    for c in widget.children[:]: 
        if fire_my_event(c): 
            return True 
 
#in your code somewhere 
class SomeWidget(Image): #or anything else ... 
    def on_touch_down(self, touch): 
       fire_my_event(self) 

玩得开心!