问题是由于
on_pre_enter
事件在添加
BoxLayout
所以它不存在,一个可能的解决方案是使用
Clock
:
class MainScreen(Screen):
def __init__(self, *args, **kwargs):
Screen.__init__(self, *args, **kwargs)
Clock.schedule_once(self.finished_init)
def finished_init(self, *args):
show_view = self.ids.rc_display # error here
show_view.clear_widgets()
buttons = BoxLayout(orientation = 'vertical')
pr = requests.get('http://127.0.0.1:5000/stageplanning/api/v1.0/shows')
for show in pr.json():
buttons.add_widget(Button(text = show['show_title']))
show_view.add_widget(buttons)
另一个选项是在BoxLayout构造函数中定义它:
*.py公司
class MainBoxLayout(BoxLayout):
def __init__(self, *args, **kwargs):
BoxLayout.__init__(self, *args, **kwargs)
self.clear_widgets()
buttons = BoxLayout(orientation = 'vertical')
pr = requests.get('http://127.0.0.1:5000/stageplanning/api/v1.0/shows')
for show in pr.json():
buttons.add_widget(Button(text = show['show_title']))
self.add_widget(buttons)
*.千伏
ScreenManager:
MainScreen:
LoginScreen:
<MainScreen>:
name: 'MainScreen'
id: ms
MainBoxLayout:
id: rc_display
orientation: "vertical"
padding: 10
spacing: 10
<LoginScreen>:
name: 'LoginScreen'
id: ls
Button:
on_release: app.root.current
text: 'Click to Login'
font_size: 20