我在应用中有以下文件
main.py
import kivy
kivy.require('1.9.0') # Kivy ver where the code has been tested!
from kivy.app import App
from kivy.uix.widget import Widget
class MyW(Widget):
def build(self):
pass
class e3App(App):
def build(self):
return MyW()
if __name__ == '__main__':
e3App().run()
e3.kv
#:import PlayListView playlistview
#:import SPlayView splayview
<MyW>:
SPlayView:
Button:
on_touch_down:self.parent.buttonPressed()
PlayListView:
splayview.py
from kivy.uix.boxlayout import BoxLayout
class SPlayView(BoxLayout):
def __init__(self,**kwargs):
super(SPlayView,self).__init__()
self.register_event_type('on_john')
def buttonPressed(self):
self.dispatch('on_john')
def on_john(self):
print 'event dispatched'
playlistview.py
from kivy.uix.boxlayout import BoxLayout
class PlayListView(BoxLayout):
def __init__(self,**kwargs):
super(PlayListView,self).__init__(**kwargs)
self.bind(on_john=self.on_john)
def on_john(self):
print 'hell'
当我按下按钮时,事件看起来好像正在调度,但它永远不会到达......我做错了什么,我是否必须以某种方式强迫事件冒泡或不被消耗,如果是这样的话
答案 0 :(得分:0)
如果您不处理碰撞,请不要使用on_touch_down
,这与on_release
或类似事件不同。
build()
是App
课程中的一项功能,下次使用__init__()
和super()
此外,您必须记住,如果您不从类中继承您想要访问该函数,它可能不起作用 - 您继承自BoxLayout
,而不是SPlayView
。< / p>
当你想要这种行为时,我可以提出两种方法:
KV
#:import PlayListView playlistview
#:import SPlayView splayview
<MyW>:
SPlayView:
Button:
PlayListView:
Button:
on_release:self.parent.buttonPressed()
使用super()
class PlayListView(SPlayView):
def __init__(self,**kwargs):
super(PlayListView,self).__init__(**kwargs)
self.bind(on_john=self.on_john)
def on_john(self):
print 'hell'
splayview:
from kivy.uix.boxlayout import BoxLayout
from splayview import SPlayView
class PlayListView(SPlayView):
def __init__(self,**kwargs):
super(PlayListView,self).__init__(**kwargs)
# self.bind(on_john=self.on_john)
def on_john(self, *args):
super(PlayListView,self).on_john()
print 'hell'
使用bind()
您不能拥有相同名称的功能
from kivy.uix.boxlayout import BoxLayout
from splayview import SPlayView
class PlayListView(SPlayView):
def __init__(self,**kwargs):
super(PlayListView,self).__init__(**kwargs)
self.bind(on_john=self.on_johnn)
def on_johnn(self, *args):
print 'hell'
你必须记住,绑定功能将首先执行,然后你的事件将被执行,例如hell
将在event dispatched
但是,如果您想从SPlayView
调用某个函数,则不必将其他函数绑定到它,只需在YourClass.function()
事件中调用on_john
即可。我有点困惑,所以我希望你的意思是这个。 O.o