在kivy中制作下拉列表。 'NoneType'对象没有属性'add_widget'

时间:2017-06-05 08:29:36

标签: python drop-down-menu kivy

我已经使用了可用的主题来改进我的代码,但仍然有些东西不起作用。运行代码会给我Attribute error:'NoneType' object has no attribute 'add_widget'

class Datownik(DropDown):
    pass

class ZleceniaPojedyncze(Screen):
    dzienButton = ObjectProperty(None)
    miesiacButton = ObjectProperty(None)
    top_layout = ObjectProperty(Screen)   <------ changed
    rokButton = ObjectProperty(None)
    dd_btn = ObjectProperty(None)

    def __init__(self, *args, **kwargs):
        super(ZleceniaPojedyncze, self).__init__(*args, **kwargs)
        self.drop_down = Datownik()

        dropdown = DropDown()
        notes = ['alpha', 'beta', 'gamma', 'delta']
        for note in notes:

            btn = Button(text='bcda', size_hint_y=None, height=30)
            btn.bind(on_release=lambda btn: dropdown.select(btn.text))
            dropdown.add_widget(btn)

        mainbutton = Button(text='abcd')
        print("yay")

        mainbutton.bind(on_release=dropdown.open(self)) <--added self param.

        dropdown.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))

        self.top_layout.add_widget(mainbutton)

这是相应的kv文件:

<Datownik>:
Button:
    text: 'B1'
    height: 44
    on_release: root.select('item1')
Button:
    text: 'B2'
    height: 44
    on_release: root.select('item1')
Button:
    text: 'B3'
    height: 44
    on_release: root.select('item1')

<ZleceniaPojedyncze>:
name: 'zp'
dzienButton: dzienButtonID
dd_btn: btn_ddID    
top_layout: topLayoutID
GridLayout:
    id: topLayoutID
    height: self.minimum_height
    row_force_default: 'true'
    row_default_height: 30
    col_force_default: 'true'
    col_default_width: 70
    cols: 4
    padding: 70
    Button:
        id: dzienButtonID
        text: 'day'
    Button:
        id: btn_ddID
        text: 'month'
        on_release: root.drop_down.open(self)
    Button:
        text: 'year'
    Button:
        text: 'back'
        on_release: app.root.current = 'mm'

我现在只想让它与一个按钮一起工作,只需下拉菜单,然后我将尝试找出哪些部分可以根据我的需要配置此菜单,但我猜我混淆了某种方式而且不能继续。 Any1有帮助吗?

1 个答案:

答案 0 :(得分:0)

您的top_layout被声明为ObjectProperty(None),然后当它为无时,您尝试向其添加小部件:self.top_layout.add_widget(mainbutton)