kivy EventDispatcher

Let's talk about kivy's EventDispatcher here:

The codes I have tried here:

kivy EventDispatcher_第1张图片

codes here:

from kivy.event import EventDispatcher
class MyEventDispatcher(EventDispatcher):
    def __init__(self, **kwargs):
        self.register_event_type('on_test')
        super(MyEventDispatcher, self).__init__(**kwargs)

    def do_something(self, value):
        # when do_something is called, the 'on_test' event will be
        # dispatched with the value
        self.dispatch('on_test', value)

    def on_test(self, *args):
        print "I am dispatched", args

 

Then I did this:

def my_callback(value, *args):
    print "Hello, I got an event!", args


ev = MyEventDispatcher()
ev.bind(on_test=my_callback)
ev.do_something('test')

 

Python returned me with this:

 

kivy EventDispatcher_第2张图片

the method "my_callback" is bounded to "on_test" of the instance "ev"

After binding, event triggers event

从上面的输出看到,执行了ev.do_something 后

 

Now if we don't bind the "my_callback", and just do 

ev.do_something('test')

let's see what we can get.

kivy EventDispatcher_第3张图片

由于没有用到dispatch 没有被先事先bind到,所以就比较难受地的给出了报错。ev.do_something   dispatch  不到 on_test 上面了。所以要事先声明一下就是bind一下。

 

kivy EventDispatcher_第4张图片

 

kivy EventDispatcher_第5张图片

你可能感兴趣的:(dispatcher)