<p>我的建议是使用一个界面管理器小部件,然后可以为UI表单使用各种小部件。</p>
<pre><code>import kivy
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
class InterfaceManager(BoxLayout):
def __init__(self, **kwargs):
super(InterfaceManager, self).__init__(**kwargs)
self.first = Button(text="First")
self.first.bind(on_press=self.show_second)
self.second = Button(text="Second")
self.second.bind(on_press=self.show_final)
self.final = Label(text="Hello World")
self.add_widget(self.first)
def show_second(self, button):
self.clear_widgets()
self.add_widget(self.second)
def show_final(self, button):
self.clear_widgets()
self.add_widget(self.final)
class MyApp(App):
def build(self):
return InterfaceManager(orientation='vertical')
if __name__ == '__main__':
MyApp().run()
</code></pre>
<p>当然,你不会那样安排的。您可以将所有表单保存在容器对象的字典中,并有一个通用回调函数,它按键提供另一个表单。</p>
<pre><code>class InterfaceManager(BoxLayout):
def __init__(self, **kwargs):
super(InterfaceManager, self).__init__(**kwargs)
self.forms = {}
def add_form(self, key, form):
self.forms[key] = form
def uniCallback(self, button):
self.clear_widgets()
# You could introduce a more elegant key
# handling system here.
self.add_widget(self.forms[button.text])
</code></pre>