我需要在 Drop-Down 中设置动态列表。对于设置,我想使用类 DiscDropDown 的构造函数,它是类 DropDown 的后代。 (这意味着,要在类 DiscDropDown 中为变量 li_disc 设置值)。这个类的实例不是python代码的一部分,因为我使用 Builder 。 我如何在下一个代码中实现它?
Builder.load_string('''
<Save_As>:
drop_down: drop_down
orientation:'vertical'
DiscDropDown:
id: drop_down
size_hint: None,None
height: 50
Button:
id: bt_delete_file
disabled: False
size_hint: 1,None
height: 250
text: 'Fictive'
''')
class DiscDropDown(DropDown):
def __init__(self, **kwarg):
super(DiscDropDown, self).__init__(**kwarg)
self.dropdown = DropDown()
# I wanted to set up value Button.text during creation Buttons
for name in li_disc:
btn = Button(text = name, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
self.dropdown.add_widget(btn)
mainbutton = Button(text='Disc', size_hint=(None, None),size = (50,50))
mainbutton.bind(on_release = self.dropdown.open)
self.dropdown.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))
self.add_widget(mainbutton)
class Save_As(BoxLayout):
# drop_down = ObjectProperty()
def __init__(self, **kwargs):
super(Save_As, self).__init__( **kwargs)
return
class ExplorerApp(App):
def build(self):
self.save_as = Save_As()
return self.save_as
if __name__ == '__main__':
ExplorerApp().run()
为了解决我的问题,我使用了下一个过程来设置值变量 Button.text :
class Save_As(BoxLayout):
drop_down = ObjectProperty()
def __init__(self, **kwargs):
li_disc = ['aa','bb','cc']
super(Save_As, self).__init__( **kwargs)
# I set up Button.text here
for i in range(0,3):
self.children[1].dropdown.children[0].children[i].text = li_disc[i]
return
我认为我的解决方案并不理想,你能推荐我更好的流程吗?