[screenB中有很多ToggleButton,在进入屏幕之前,我要重置所有的ToggleButton。
class screenB(Screen):
def __init__(self, **kwargs):
super(MorePage, self).__init__(**kwargs)
Clock.schedule_once(self.AddWidget)
def AddWidget(self, *args):
for i in range(len(all_brand)):
brand_name = all_brand[i]
if brand_name[0] == "A" or brand_name[0] == "a":
self.tbutton = MyToggle(id=brand_name, text=brand_name)
self.ids.Axxx.add_widget(self.tbutton)
print(self.tbutton.id)
elif brand_name[0] == "B" or brand_name[0] == "b":
self.tbutton = MyToggle(id=brand_name, text=brand_name)
self.ids.Bxxx.add_widget(self.tbutton)
elif brand_name[0] == "C" or brand_name[0] == "c":
self.tbutton = MyToggle(id=brand_name, text=brand_name)
self.ids.Cxxx.add_widget(self.tbutton)
....
def reset_state(self):
print(self.ids)
print(self.ids.Axxx.ids)
for i in all_brand: # all brand includes all ids of ToggleButton added in AddWidget
self.ids[i].state = "normal"
在.kv文件中,
<screenB>:
on_pre_enter:
root.reset_state()
Label:
id: aaa
Label:
id:bbb
StackLayout:
id: Axxx
StackLayout:
id: Bxxx
StackLayout:
id: Cxxx
...
我尝试进入screenB之前先呼叫reset_state
。然后,我发现self.ids
仅给了我aaa,bbb,Axxx,Bxxx,Cxxx,但没有在AddWidget
中添加任何内容。 self.ids.Axxx.ids
给了我空洞的命令。但是,如果在添加ToggleButton之后立即打印tbutton.id,则会得到当前的返回值。
然后如何重置所有ToggleButton的状态?非常感谢!
[我想我现在知道,self.Axxx.ids
不返回任何内容,因为在该小部件中您没有任何带有id
的孩子,只需执行此操作
for child in self.ids.Axxx.children:
child.state = "normal"
应该这样做