【发布时间】:2017-08-14 11:45:11
【问题描述】:
从python代码(动态)将小部件添加到屏幕的好方法是什么?
我的目标是拥有两个“主”屏幕。第二个屏幕的内容取决于在主屏幕上选择的内容。为简单起见,可以将此内容视为可变数量的按钮(所有内容,如按钮数量、背景图像等都存储在数据库中并独立更新)。所以我想象的逻辑如下:当按下 MainScreen 按钮时,我进入数据库并通过键“按钮 N”检索数据,并以自定义 Button/Label 小部件的形式将其显示在第二个屏幕上。如果需要,我还想返回 MainScreen 以在其中选择另一个按钮。
假设我有两个屏幕,它们在 KV 文件中定义了静态图形:
<MainScreen>
BoxLayout:
orientation: "vertical"
Label:
text: "SELECT BUTTON"
Button:
on_press: root.show_buttons(self)
text: "Button 1"
Button:
on_press: root.show_buttons(self)
text: "Button 2"
<AnotherScreen>:
BoxLayout:
orientation: 'vertical'
Label:
text: "Select BUTTON"
Button:
on_press: root.manager.current = 'Main'
text: "BACK"
MainScreen 是完全静态的。当按下按钮时,我需要调用 AnotherScreen 并根据按下的按钮向其中添加内容。 我尝试按以下方式进行操作(但失败了):
class MainScreen(Screen):
def show_buttons(self, btn):
if btn.text == 'Button 1':
another_screen.add_widget(Label(text="Button 1 pressed"))
sm.current = "Another"
return sm
class AnotherScreen(Screen):
pass
class CoolApp(App):
def build(self):
global sm
sm = ScreenManager()
main_screen = MainScreen(name='Main')
sm.add_widget(main_screen)
global another_screen
another_screen = AnotherScreen(name='Another')
sm.add_widget(another_screen)
return sm
if __name__ == '__main__':
CoolApp().run()
在这种情况下,动态小部件another_screen.add_widget(Label(text="Button pressed")) 被添加到根据定义只能有一个孩子的屏幕(已在 KV 中定义)。我没有得到异常,但是我同时渲染了两个不同的小部件,这同样糟糕。
我想我需要在 BoxLayout 中添加动态小部件,因为它是该屏幕的根小部件。但是,我不知道是否可以引用不同类的小部件,如果可以,是否合适?
谢谢!
【问题讨论】:
标签: python kivy kivy-language