我正在尝试使用on_start
方法将小部件添加到屏幕,但是我遇到了尝试访问的屏幕ID出现键错误的情况。因此,我打印出self.root.ids
,它是一个空字典。
这与on_start
方法中未填充的屏幕有关吗?
main.py
class SelectChat(Screen):
pass
class MyApp(MDApp):
def build(self):
kv = Builder.load_file("kivy.kv")
self.sm = WindowManager()
screens = [SelectChat(name="select_chat")]
for screen in screens:
self.sm.add_widget(screen)
self.sm.current = "select_chat"
return self.sm
def on_start(self):
self.docs = self.my_firestore.db.collection(u'users').where(u'value', u'==', True).stream()
print(self.root.ids) #This is producing the empty dict
#select_chat = self.root.ids['select_chat'].ids['select_chat_grid'] # This is where I get the key error
main.kv
<SelectChat>:
name: "select_chat"
id: select_chat
GridLayout:
cols: 1
id: select_chat_grid
select_chat = self.root.ids['select_chat'].ids['select_chat_grid']
至:
select_chat = self.root.get_screen("select_chat").ids['select_chat_grid']
所以我必须引用屏幕名称,而不是屏幕ID