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

    1. <small id='n9XVR'></small><noframes id='n9XVR'>

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

      Kivy:如何通过 id 获取小部件(没有 kv)

      Kivy : how to get widget by id (without kv)(Kivy:如何通过 id 获取小部件(没有 kv))
        • <bdo id='QEkes'></bdo><ul id='QEkes'></ul>

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

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

              <legend id='QEkes'><style id='QEkes'><dir id='QEkes'><q id='QEkes'></q></dir></style></legend>
              <tfoot id='QEkes'></tfoot>

                  <tbody id='QEkes'></tbody>
                本文介绍了Kivy:如何通过 id 获取小部件(没有 kv)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我在 Kivy 中动态定义了一些小部件(按钮)并动态分配它们的 ID.在这个用例中我没有使用 kv 语言.我可以在不跟踪小部件本身的情况下保留小部件 ID 的引用:然后我想通过它的 ID 访问小部件.我可以做类似通过 id 获取小部件"之类的事情吗?(如果我在 kv 文件中定义了小部件,我可以使用 self.ids.the_widget_id 通过其 id 访问小部件本身)

                Let's say I define on the fly in Kivy a few widgets (buttons) and dynamically assign their id. I'm not using kv language in this use case. I can keep a reference of a widget id without keeping track of the widget itself : then I'd like to access the widget through its id. Can I do something like "get widget by id" ? (If I had defined the widget in a kv file, I could have used self.ids.the_widget_id to access the widget itself through its id)

                推荐答案

                Kivy 小部件制作树形结构.任何小部件的子级都可以通过 children 属性获得.如果需要,您可以只保留对根窗口的引用,然后使用 walk 方法迭代它的小部件:

                Kivy widgets make tree structure. Children of any widget are avaiable through children atribute. If you want, you can keep reference only to root window and then iterate over it's widgets using walk method:

                from kivy.app import App
                from kivy.uix.boxlayout import BoxLayout
                from kivy.uix.button import Button 
                
                class MyWidget(BoxLayout):
                    def __init__(self, **kwargs):
                        super().__init__(**kwargs)
                
                        button = Button(text="...", id="1")
                        button.bind(on_release=self.print_label)
                
                        l1 = BoxLayout(id="2")
                        l2 = BoxLayout(id="3")
                
                        self.add_widget(l1)
                        l1.add_widget(l2)             
                        l2.add_widget(button)
                
                    def print_label(self, *args):
                        for widget in self.walk():
                            print("{} -> {}".format(widget, widget.id))
                
                class MyApp(App):
                    def build(self):
                        return MyWidget()
                
                if __name__ == '__main__':
                    MyApp().run()
                

                walk()walk_reverse() 方法被添加到 Kivy 1.8.1 版本的 kivy.uix.widget.Widget 中.对于旧版本,您需要自己递归解析树:

                walk() and walk_reverse() method were added to kivy.uix.widget.Widget in 1.8.1 version of Kivy. For older versions you need to recursively parse tree yourself:

                from kivy.app import App
                from kivy.uix.boxlayout import BoxLayout
                from kivy.uix.button import Button 
                
                class MyWidget(BoxLayout):
                    def __init__(self, **kwargs):
                        super().__init__(**kwargs)
                
                        button = Button(text="...", id="1")
                        button.bind(on_release=self.print_label)
                
                        l1 = BoxLayout(id="2")
                        l2 = BoxLayout(id="3")
                
                        self.add_widget(l1)
                        l1.add_widget(l2)             
                        l2.add_widget(button)
                
                    def print_label(self, *args):
                        children = self.children[:]
                        while children:
                            child = children.pop()
                            print("{} -> {}".format(child, child.id))
                            children.extend(child.children)
                
                class MyApp(App):
                    def build(self):
                        return MyWidget()
                
                if __name__ == '__main__':
                    MyApp().run()
                

                这篇关于Kivy:如何通过 id 获取小部件(没有 kv)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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中更改按钮或标签文本颜色)
                  • <bdo id='BhWJx'></bdo><ul id='BhWJx'></ul>

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

                      <tbody id='BhWJx'></tbody>

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

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