在Kivy中动态构建下拉菜单 [英] Building Dropdowns Dynamically in Kivy

查看:639
本文介绍了在Kivy中动态构建下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于一个类的dictionary属性在kivy中创建下拉列表.但是,关于在循环中创建它们的某些事情会使Kivy感到困惑,或者我感到困惑.这是我所拥有的:

I'd like to create dropdown-lists in kivy based on a dictionary property of a class. But something about creating these in a loop is either confusing Kivy or I am just confused. Here's what I have:

for main, ingrediants in self.ingrediants.items():
    print main, ingrediants
    dropdown = DropDown()

    for ingrediant in ingrediants:
        btn = Button(text=ingrediant, size_hint_y=None, height=44)
        btn.bind(on_release=lambda btn: dropdown.select(btn.text))
        dropdown.add_widget(btn)

    trigger = Button(text=main, size_hint=(None, None))
    trigger.bind(on_release=dropdown.open)
    dropdown.bind(on_select=lambda instance, x: setattr(trigger, 'text', x))

    self.toolbar.dropdown_bar.add_widget(trigger)

这是我的财产的样子:

ingrediants = DictProperty(
    {
        'Milk': ['Whole Milk', 'Soy', 'Creamer'],
        'Coffee': ['Drip Coffee', 'Espresso', 'Pour Over'],
        'Sugar': ['Sugar', 'Simple Syrup', 'Raw Sugar'],
    }
)

渲染后,下拉菜单看起来正确,但是三个按钮,但是,牛奶一个不会触发下拉菜单,咖啡一个会触发下拉菜单,但是选中后,更改糖按钮的文本,第三个按钮可以正常工作,触发下拉菜单并更改所选内容上的按钮文字.

When this renders, the dropdown bar looks correct, three buttons, BUT, the milk one does not trigger a dropdown, the coffee one triggers it's dropdown, but when selected, changes the sugar button's text, and the third button works normally, triggering a dropdown and changing the buttons text on selection.

我觉得我的循环做错了什么.但是也许您不能这样声明下拉列表?谢谢.

I feel like I'm just doing something wrong with my loop. But maybe you can't declare dropdowns like this? Thanks.

这是我要使其正常工作所要做的.

Here's what I had to do to get it working.

dropdowns = {}
for main, ingrediants in self.ingrediants.iteritems():
    dropdowns[main] = DropDown()

    for ingrediant in ingrediants:
        btn = Button(text=ingrediant, size_hint_y=None, height=44)
        btn.bind(on_release=lambda btn=btn, dropdown=dropdowns[main]: dropdown.select(btn.text))
        dropdowns[main].add_widget(btn)

    trigger = Button(text=main, size_hint=(None, 1))

    trigger.bind(on_release=dropdowns[main].open)
    dropdowns[main].bind(on_select=lambda instance, x, trigger=trigger: setattr(trigger, 'text', x))
    self.toolbar.dropdown_bar.add_widget(trigger)

推荐答案

我很确定您的问题主要与lambda函数在for循环中的行为有关.例如,您可以查看上一个问题,以了解原因-简短答案,每个lambda接收相同的变量,因此只有该变量(最后一个下拉列表)会执行任何操作.

I'm pretty sure your problems are mostly to do with the way lambda functions behave in for loops. You can see for instance this previous question for information on why - short answer, every lambda receives the same variable, so only that variable (the last dropdown) does anything.

我没有时间创建一个有效的示例(很奇怪,您没有提供初始的有效示例),但是如果这还不足以解决您的问题,我将尝试再做一个.问题.

I didn't have time to create a working example (it's fiddly, and you didn't provide an initial working example), but I'll try to make one later if this isn't enough for you to fix the issue.

我也有一个下拉菜单无法正常工作的问题,但是我认为这是因为您不存储对它们的引用,因此它们会被垃圾收集.我添加了dropdowns = ListProperty([])self.dropdowns.append(dropdown)来保留引用,从而解决了它们不出现的问题.

I also had a problem with dropdowns not working, but I think that's because you don't store references to them so they get garbage collected. I added dropdowns = ListProperty([]) and self.dropdowns.append(dropdown) to keep references around, which solved the problem of them not appearing.

这篇关于在Kivy中动态构建下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆