【发布时间】: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()
【问题讨论】: