如果您的定位目标不是太复杂,aRelativeLayout
可能就足够了。
class MainPage(RelativeLayout):
def __init__(self, dispatcher, **kwargs):
super(MainPage, self).__init__(**kwargs)
self.child = ChildWidget(dispatcher, pos=(10,10))
self.add_widget(self.child)
(RelativeLayout
通常是任何布局 - 除了保持绝对定位的 FloatLayout )保持子级更新到父级的位置。所有的布局也是小部件,所以你应该是安全的。
<ChildWidget>:
size_hint: .1, .05
text: "press me"
on_press: self.parent.pos = (200,200)
<MainPage>:
pos: 400,400
这是我尝试过的完整代码:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
Builder.load_string("""
<ChildWidget>:
size_hint: .1, .05
text: "press me"
on_press: self.parent.pos = (200,200)
<MainPage>:
pos: 400,400
""")
class Dispatcher():
pass
class ChildWidget(Button):
def __init__(self, dispatcher, **kwargs):
super(ChildWidget, self).__init__(**kwargs)
self.dispatcher = dispatcher
class MainPage(RelativeLayout):
def __init__(self, dispatcher, **kwargs):
super(MainPage, self).__init__(**kwargs)
self.child = ChildWidget(dispatcher, pos=(10,10))
self.add_widget(self.child)
class TestApp(App):
def build(self):
dispatcher = Widget()
return MainPage(dispatcher)
if __name__ == '__main__':
TestApp().run()