进入屏幕之前如何重设ToggleButton的状态?

问题描述 投票:0回答:1

[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的状态?非常感谢!

python kivy togglebutton
1个回答
0
投票

[我想我现在知道,self.Axxx.ids不返回任何内容,因为在该小部件中您没有任何带有id的孩子,只需执行此操作

for child in self.ids.Axxx.children:
    child.state = "normal"

应该这样做

© www.soinside.com 2019 - 2024. All rights reserved.