我想在Python中使用Kivy
库而不是使用Kivy语言构建器在布局中创建布局,如下所示:
Builder.load_string("""
<ListScreen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: .1
Button:
text: 'Save'
Button:
text: 'Load'
Button:
text: 'New'
Button:
text: 'New'
on_press:
root.manager.transition.direction = 'down'
root.manager.current = 'search'
""")
我很难弄清楚如何访问第二个布局以向其添加小部件。示例代码如下:
class ListScreen(Screen):
layout = BoxLayout(orientation ='vertical')
layout.add_widget(top_buttons=BoxLayout())
top_buttons.add_widget(save=Button(text='Save')
# ^ problem here, it won't recognize top_buttons
正如您所看到的,我尝试访问布局之外的top_buttons,但我无法弄清楚如何到达它,因此我无法添加我的小部件。我该如何访问它以添加小部件?
答案 0 :(得分:0)
class ListScreen(Screen):
layout = BoxLayout(orientation ='vertical')
layout.add_widget(top_buttons=BoxLayout()) # <--------
top_buttons.add_widget(save=Button(text='Save')
正是因为该行,您才能使用变量top_buttons
,因为您将其用作关键字,而不是变量。它应该是这样的:
class ListScreen(Screen):
layout = BoxLayout(orientation ='vertical')
top_buttons=BoxLayout()
layout.add_widget(top_buttons) # <--------
top_buttons.add_widget(save=Button(text='Save')