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

  • <tfoot id='xq5Eu'></tfoot>

    <small id='xq5Eu'></small><noframes id='xq5Eu'>

    <legend id='xq5Eu'><style id='xq5Eu'><dir id='xq5Eu'><q id='xq5Eu'></q></dir></style></legend>
      <bdo id='xq5Eu'></bdo><ul id='xq5Eu'></ul>

      1. 引用 Kivy 中动态创建的小部件的 id

        Referencing id of dynamically created widget in Kivy(引用 Kivy 中动态创建的小部件的 id)
      2. <legend id='JpF9I'><style id='JpF9I'><dir id='JpF9I'><q id='JpF9I'></q></dir></style></legend>

              <tbody id='JpF9I'></tbody>
            • <bdo id='JpF9I'></bdo><ul id='JpF9I'></ul>

              <small id='JpF9I'></small><noframes id='JpF9I'>

              <tfoot id='JpF9I'></tfoot>

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

                1. 本文介绍了引用 Kivy 中动态创建的小部件的 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法通过 root.ids.created_in_kv.created_in_py 在绑定到按钮的方法中访问动态创建的子项.当我检查 root.ids.created_in_kv.ids 字典时它是空的,但是 root.ids.created_in_kv.children

                  I am having trouble accessing dynamically created children via root.ids.created_in_kv.created_in_py in method bound to button. When I check root.ids.created_in_kv.ids dictionary it is empty, but there are children in root.ids.created_in_kv.children

                  我想要实现的是创建一个充当多选器的弹出窗口.它将接受可能的选择并动态创建标签-复选框对并将其添加到弹出内容中,并且在应用"按钮上它将仅返回选择的列表(str()).

                  What I'm trying to achieve is to create a popup that will act as multiselector. It will accept possible choices and dynamically create label-checkbox pair and add it to popup contents, and on 'Apply' button it will return only chosen list(str()).

                  我无法在 kv 中构建包含多个小部件的弹出窗口,但以下工作(建议使其更好"而不是受欢迎):

                  I can't constuct popup with multiple widgets in it in kv, but the following works (suggestions to make it 'nicer' more than welcome):

                  kv代码:

                  <SelectorPopup>:
                      title: 'empty'
                      BoxLayout:
                          id: inside
                          orientation: 'vertical'
                          BoxLayout:
                              id: options
                          BoxLayout:
                              id: buttons
                              orientation: 'vertical'
                              Button:
                                  text: 'Apply'
                                  on_release: root.return_selected()
                              Button:
                                  text: 'Cancel'
                                  on_release: root.dismiss()
                  
                  <LabeledCheckbox@BoxLayout>:
                      id: entity
                      CheckBox:
                          id: choice
                      Label:
                          text: root.id
                  

                  我正在创建标签-复选框对(打包在 GridLayout 中)并将其放入 options BoxLayout 的 python 代码:

                  And the python code I'm creating the label-checkbox pairs (packaged in GridLayout) and putting it into options BoxLayout:

                  class SelectorPopup(Popup):
                      def return_selected(self):
                          selected=[]
                          a = self.ids.inside.options.choices.ids # dict is empty
                          for item in a.keys():
                               selected.append(item) if a[item].ids.choice.value #add if checkbox checked
                          return selected
                  
                  class MultiselectForm(BoxLayout):
                      data = ListProperty([])
                      def __init__(self, **kwargs):
                          super(MultiselectForm, self).__init__(**kwargs)
                          self.prompt = SelectorPopup()
                  
                      def apply_data(self):
                          # write data to self.data
                          pass
                  
                      def create_popup(self):
                          try:
                              # some code to check if choices are already created
                              check = self.prompt.ids.inside.options.choices.id
                          except AttributeError:
                              possible = ['choice1','choice2','choice3'] #query db for possible instances
                              choices = GridLayout(id='choices',cols=2)
                              for entity in possible:
                                  choice = Factory.LabeledCheckbox(id=entity)
                                  choices.add_widget(choice)
                              self.prompt.ids.options.add_widget(choices)
                          self.prompt.open()
                  

                  问题:

                  1) 如何使 return_selected 方法起作用?

                  1) Howto make return_selected method work?

                  2) 有没有办法更好地构建弹出窗口?我无法将小部件树添加到 content ObjectProperty 中,例如:

                  2) Is there a way to construct popup more nicely? I can't add widget tree into content ObjectProperty like:

                  <MyPopup>:
                      content:
                          BoxLayout:
                              Label:
                                  text: 'aaa'
                              Label:
                                  text: 'bbb'
                  

                  推荐答案

                  看起来你对 ids 的工作方式有点混淆.文档中对它们进行了一些讨论:https://kivy.org/docs/api-kivy.lang.html

                  It looks like you're a little mixed up on how ids work. They are talked about a little bit in the docs: https://kivy.org/docs/api-kivy.lang.html

                  基本上,它们只是 .kv 中的特殊标记,可让您引用已定义的小部件.它们被收集并放置在定义它们的规则的ids字典根小部件中.这意味着它们不像您引用它们那样嵌套,它们是所有在根窗口小部件上(SelectorPopupLabeledCheckbox)

                  Basically, they are just special tokens in .kv that lets you reference defined widgets. They are collected and placed in the ids dictionary on the root widget of the rule they are defined in. This means they aren't nested like you are referencing them, they are all on the root widget (SelectorPopup or LabeledCheckbox)

                  所以而不是(从 SelectorPopup 中):

                  So instead of (from within SelectorPopup):

                  self.ids.inside.options.choices
                  

                  你会:

                  self.ids.choices
                  

                  这也意味着动态添加的小部件不会出现在 ids 字典中,但它们实际上并不需要.由于您是在代码中创建它们,因此您可以自己保存对它们的引用(使用 .kv 更难做到这一点).

                  This also means that widgets added dynamically aren't going to be present in the ids dictionary, but they don't really need to be. Since you are creating them in code, you can just save references to them yourself (which is harder to do with .kv).

                  话虽如此,使用 ListView 显示您的项目列表.

                  All that being said, it might be a lot easier to use a ListView to display your list of items.

                  这篇关于引用 Kivy 中动态创建的小部件的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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中更改按钮或标签文本颜色)

                  <small id='ydMID'></small><noframes id='ydMID'>

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

                        <tbody id='ydMID'></tbody>

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

                          • <tfoot id='ydMID'></tfoot>