【问题标题】:What kind of kivy widgets can I use to implement an android phone app like UI?我可以使用什么样的 kivy 小部件来实现类似 UI 的 Android 手机应用程序?
【发布时间】:2013-07-01 09:11:38
【问题描述】:

我正在学习编写我的第一个 kivy 应用程序。我想实现一种android电话应用程序的UI,即它在屏幕底部有几个按钮,当按下一个按钮时,上面的屏幕会变成相应的内容(可能是一个记录列表,或经典设置屏幕)。

我想我可能需要两个自定义的容器小部件用于按钮组和上述内容,而内容小部件可能需要动态删除所有子小部件并为按钮 on_press 事件添加相应的新子小部件。我不确定这里的想法是否合适,或者对于这种使用 kivy 的经典 android 应用程序 ui 有现成的解决方案?

【问题讨论】:

    标签: kivy


    【解决方案1】:

    我认为您要的是ScreenManager。 Kivy 提供了一个很好的例子,但这里是另一个例子。我认为它与您正在寻找的更相似:

    从 kivy.app 导入应用 从 kivy.lang 导入生成器 从 kivy.uix.floatlayout 导入 FloatLayout

    Builder.load_string("""
    <Phone>:
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'top'
            ScreenManager:
                size_hint: 1, .9
                id: _screen_manager
                Screen:
                    name: 'screen1'
                    Label: 
                        text: 'The first screen'
                Screen:
                    name: 'screen2'
                    Label: 
                        text: 'The second screen'
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'bottom'
            BoxLayout:
                orientation: 'horizontal'
                size_hint: 1, .1
                Button:
                    text: 'Go to Screen 1'
                    on_press: _screen_manager.current = 'screen1'
                Button:
                    text: 'Go to Screen 2'
                    on_press: _screen_manager.current = 'screen2'""")
    
    class Phone(FloatLayout):
        pass
    
    class TestApp(App):
        def build(self):
            return Phone()
    
    if __name__ == '__main__':
        TestApp().run()
    

    【讨论】:

    • 感谢 toto_tico,这与我想要的非常相似,但是我想在通过 on_press 更改上述内容屏幕时保持按钮不变。无论如何,我认为 ScreenManager 应该是选择,再次感谢。
    • 当然。 ScreenManager 本身就是 Widget,可以随心所欲。我修改了代码,所以现在你得到了你想要的。我第一次没有很好地理解这个问题,对不起。如果您认为这现在回答了您的问题,请在此处标记。 StackOverflow About
    猜你喜欢
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    相关资源
    最近更新 更多