kivy EventDispatcher

Let's talk about kivy's EventDispatcher here:

The codes I have tried here:

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:

 

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.

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

 

 

转载于:https://www.cnblogs.com/spaceship9/p/3455335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值