<tfoot id='4CuOu'></tfoot>
    <legend id='4CuOu'><style id='4CuOu'><dir id='4CuOu'><q id='4CuOu'></q></dir></style></legend>
    • <bdo id='4CuOu'></bdo><ul id='4CuOu'></ul>
  1. <small id='4CuOu'></small><noframes id='4CuOu'>

    1. <i id='4CuOu'><tr id='4CuOu'><dt id='4CuOu'><q id='4CuOu'><span id='4CuOu'><b id='4CuOu'><form id='4CuOu'><ins id='4CuOu'></ins><ul id='4CuOu'></ul><sub id='4CuOu'></sub></form><legend id='4CuOu'></legend><bdo id='4CuOu'><pre id='4CuOu'><center id='4CuOu'></center></pre></bdo></b><th id='4CuOu'></th></span></q></dt></tr></i><div id='4CuOu'><tfoot id='4CuOu'></tfoot><dl id='4CuOu'><fieldset id='4CuOu'></fieldset></dl></div>

      在 Kivy 中动态构建下拉菜单

      Building Dropdowns Dynamically in Kivy(在 Kivy 中动态构建下拉菜单)
    2. <small id='dvsq4'></small><noframes id='dvsq4'>

        <bdo id='dvsq4'></bdo><ul id='dvsq4'></ul>

        <legend id='dvsq4'><style id='dvsq4'><dir id='dvsq4'><q id='dvsq4'></q></dir></style></legend>
          <tbody id='dvsq4'></tbody>
            <tfoot id='dvsq4'></tfoot>

              • <i id='dvsq4'><tr id='dvsq4'><dt id='dvsq4'><q id='dvsq4'><span id='dvsq4'><b id='dvsq4'><form id='dvsq4'><ins id='dvsq4'></ins><ul id='dvsq4'></ul><sub id='dvsq4'></sub></form><legend id='dvsq4'></legend><bdo id='dvsq4'><pre id='dvsq4'><center id='dvsq4'></center></pre></bdo></b><th id='dvsq4'></th></span></q></dt></tr></i><div id='dvsq4'><tfoot id='dvsq4'></tfoot><dl id='dvsq4'><fieldset id='dvsq4'></fieldset></dl></div>

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

                问题描述

                我想根据类的字典属性在 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 循环中的行为方式有关.例如,您可以查看 this 上一个问题以获取有关原因的信息 - 简短回答,每个 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 中动态构建下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                Centering an object in Kivy(在 Kivy 中将对象居中)
                How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                <tfoot id='BcPgm'></tfoot>
                    <tbody id='BcPgm'></tbody>
                  1. <i id='BcPgm'><tr id='BcPgm'><dt id='BcPgm'><q id='BcPgm'><span id='BcPgm'><b id='BcPgm'><form id='BcPgm'><ins id='BcPgm'></ins><ul id='BcPgm'></ul><sub id='BcPgm'></sub></form><legend id='BcPgm'></legend><bdo id='BcPgm'><pre id='BcPgm'><center id='BcPgm'></center></pre></bdo></b><th id='BcPgm'></th></span></q></dt></tr></i><div id='BcPgm'><tfoot id='BcPgm'></tfoot><dl id='BcPgm'><fieldset id='BcPgm'></fieldset></dl></div>
                      <bdo id='BcPgm'></bdo><ul id='BcPgm'></ul>
                      • <small id='BcPgm'></small><noframes id='BcPgm'>

                        <legend id='BcPgm'><style id='BcPgm'><dir id='BcPgm'><q id='BcPgm'></q></dir></style></legend>