【问题标题】:Getting a kivy app to update visible interface (no widgets)获取 kivy 应用程序以更新可见界面(无小部件)
【发布时间】:2018-03-21 00:42:28
【问题描述】:

我正在使用kivy,无法更新kivy界面。

我的布局基于 current_node,这是一个用于填充其他变量的字典条目。理想情况下,布局应该取决于当前节点。

当我运行应用程序时,current_node 及其所有子变量在 python 代码中的 MyApp 类中更新 - 但不是在 kivy 界面上。任何想法为什么?

我试过的东西没有用:

  • 为布局创建一个单独的类(不能动态改变按钮的数量)
  • update_node 函数更改为包含 MyApp.build()(缺少位置参数 self)

提前感谢您的任何建议。

tree = {
    '0' : ['Hi!', 'A', 'B'],
    'A' : ['Yes', 'AA', 'AB','AC'],
    'AA': ['Seneca', 'AAA', 'AAB'],
    'AAA': ['Yes', 'AAA', 'AAB'],
    'AAB': ['No', 'AAA', 'AAB'],
    'AB' : ['Cato', "AA", "AB"],
    'AC' : ['Neither'],
    'B' : ["No",'BA','BB'],
    'BA': ['xx'],
    'BB': ['xxx']
}

class MyApp(App):
    current_node = '0'

    def update_node(self, *args):
        self.current_node = args[0]
        self.build()

    def build(self):
        layout = FloatLayout()
        child_nodes = tree[self.current_node][1:]
        j = len(child_nodes)
        # Answer Buttons
        for i in child_nodes:
            answer_button = Button(
                    text=tree[i][0],
                    pos=(100, j*75),
                    size_hint = (0.8, 0.1),
                    )
            button_callback = partial(self.update_node, i)
            answer_button.bind(on_release=button_callback)
            layout.add_widget(answer_button)
            j -= 1
            print(i)
        return layout

if __name__ == '__main__':
    MyApp().run()

【问题讨论】:

    标签: python kivy


    【解决方案1】:
    def update_node(self, *args):
        self.current_node = args[0]
        self.build()
    

    self.build() - is 一种 A​​pp 本身调用以获取根小部件的方法。你不应该手动调用它,你不能用它重新创建东西。

    您应该改为更改根小部件的子级:

    from functools import partial
    
    from kivy.app import App
    
    from kivy.uix.button import Button
    from kivy.uix.floatlayout import FloatLayout
    
    
    tree = {
        '0':   ['Hi!', 'A', 'B'],
        'A':   ['Yes', 'AA', 'AB','AC'],
        'AA':  ['Seneca', 'AAA', 'AAB'],
        'AAA': ['Yes', 'AAA', 'AAB'],
        'AAB': ['No', 'AAA', 'AAB'],
        'AB':  ['Cato', "AA", "AB"],
        'AC':  ['Neither'],
        'B':   ["No",'BA','BB'],
        'BA':  ['xx'],
        'BB':  ['xxx']
    }
    
    
    class RootWidget(FloatLayout):
        current_node = '0'
    
        def __init__(self, **kwargs):
            super(FloatLayout, self).__init__(**kwargs)
            self._rebuild()
    
        def update_node(self, i, *args):
            self.current_node = i
            self._rebuild()
    
        def _rebuild(self, *args):
            self.clear_widgets()  # clear current buttons
    
            child_nodes = tree[self.current_node][1:]
            j = len(child_nodes)
            # Answer Buttons
            for i in child_nodes:
                answer_button = Button(
                    pos=(100, j*75),
                    size_hint=(0.8, 0.1),
                    text=tree[i][0],
                )
                answer_button.bind(
                    on_release=partial(self.update_node, i)
                )
                j -= 1
                print(i)
                self.add_widget(answer_button)
    
    
    class MyApp(App):
        def build(self):
            return RootWidget()
    
    
    if __name__ == '__main__':
        MyApp().run()
    

    【讨论】:

    • 谢谢!这个解释和例子非常有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    相关资源
    最近更新 更多