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

        <bdo id='3u1vS'></bdo><ul id='3u1vS'></ul>

        <legend id='3u1vS'><style id='3u1vS'><dir id='3u1vS'><q id='3u1vS'></q></dir></style></legend>

        <small id='3u1vS'></small><noframes id='3u1vS'>

      1. 具有动态网格布局的 Kivy 模板

        Kivy template with dynamic grid layout(具有动态网格布局的 Kivy 模板)
      2. <i id='3dFvO'><tr id='3dFvO'><dt id='3dFvO'><q id='3dFvO'><span id='3dFvO'><b id='3dFvO'><form id='3dFvO'><ins id='3dFvO'></ins><ul id='3dFvO'></ul><sub id='3dFvO'></sub></form><legend id='3dFvO'></legend><bdo id='3dFvO'><pre id='3dFvO'><center id='3dFvO'></center></pre></bdo></b><th id='3dFvO'></th></span></q></dt></tr></i><div id='3dFvO'><tfoot id='3dFvO'></tfoot><dl id='3dFvO'><fieldset id='3dFvO'></fieldset></dl></div>
      3. <legend id='3dFvO'><style id='3dFvO'><dir id='3dFvO'><q id='3dFvO'></q></dir></style></legend>

            <small id='3dFvO'></small><noframes id='3dFvO'>

          • <tfoot id='3dFvO'></tfoot>

            • <bdo id='3dFvO'></bdo><ul id='3dFvO'></ul>

                    <tbody id='3dFvO'></tbody>
                  本文介绍了具有动态网格布局的 Kivy 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试为布局创建一个模板,如下所示:

                  I'm trying to create a template for a layout which looks like the following:

                  |----------|
                  |          | 
                  | IMAGE    |   <--- Just an image (square)
                  |          |
                  |----------| 
                  |[btn][btn]|   <--- GridLayout cols=2 of buttons 
                  |[btn][btn]| 
                  |[btn][btn]| 
                  |[btn][btn]| 
                  |[btn][btn]| 
                  |[btn][btn]|
                  |----------|
                  

                  第一部分很简单(但我可能错了,因为我是 kivy 的新手)

                  the first part is easy (but I could be wrong, as I'm very new at kivy)

                  #:kivy 1.6
                  [SideBar@BoxLayout]:
                      orientation: 'vertical'
                      Image:
                          source: ctx.image
                          size_hint: (1, None)
                          height: root.width
                      GridLayout:
                          cols: 2
                          # What do I do here to make it easy to load a list of buttons?
                  

                  推荐答案

                  #!/usr/bin/env python2
                  from kivy.app import App
                  from kivy.lang import Builder
                  from kivy.uix.floatlayout import FloatLayout
                  from kivy.uix.button import Button
                  
                  Builder.load_string('''
                  #:kivy 1.6
                  [SideBar@BoxLayout]:
                      content: content
                      orientation: 'vertical'
                      size_hint: ctx.size_hint if hasattr(ctx, 'size_hint') else (1, 1)
                      Image:
                          source: ctx.image
                          size_hint: (1, None)
                          height: root.width
                      GridLayout:
                          cols: 2
                          # just add a id that can be accessed later on
                          id: content
                  
                  <Root>:
                      Button:
                          center_x: root.center_x
                          text: 'press to add_widgets'
                          size_hint: .2, .2
                          on_press:
                              # what comes after `:` is basically normal python code
                              sb.content.clear_widgets()
                              # however using a callback that you can control in python
                              # gives you more control
                              root.load_content(sb.content)
                      SideBar:
                          id: sb
                          size_hint: .2, 1
                          image: 'data/images/image-loading.gif'
                  ''')
                  
                  class Root(FloatLayout):
                  
                      def load_content(self, content):
                          for but in range(20):
                              content.add_widget(Button(
                                                  text=str(but)))
                  
                  class MyApp(App):
                      def build(self):
                          return Root()
                  
                  if __name__ == '__main__':
                      MyApp().run()
                  

                  我希望内联注释使示例足够清晰.在这种情况下,我们只是将内容的 ref 传递给将小部件添加到内容的函数.

                  I hope the inline comments make the example clear enough. In this case we are just passing the ref of the content to the function that adds widgets to the content.

                  在某些情况下,您可能希望访问 Widget 作为您自己类的属性.在这种情况下,您可以使用以下 方法.

                  There are situations where you might want to have access to the Widget as a attribute of your own class. In that case you can Use the following method.

                  上面的方法基本上是加了一个ObjectProperty的与 id 同名,将 id 所引用的小部件的引用传递给它.因此,您现在拥有一个与 python 类中的 id 同名的属性,以便于访问.使用上述方法,您的代码将如下所示.

                  The method above basically adds a ObjectProperty of the same name as the id, passes the reference of the widget referenced to by the id to it. So you, now have a attribute with the same name as the id in your python class for easy access. Using the above mentioned method your code would look something like this.

                  #!/usr/bin/env python2
                  from kivy.app import App
                  from kivy.lang import Builder
                  from kivy.uix.floatlayout import FloatLayout
                  from kivy.uix.button import Button
                  from kivy.properties import ObjectProperty
                  
                  Builder.load_string('''
                  #:kivy 1.6
                  [SideBar@BoxLayout]:
                      content: content
                      orientation: 'vertical'
                      size_hint: ctx.size_hint if hasattr(ctx, 'size_hint') else (1, 1)
                      Image:
                          source: ctx.image
                          size_hint: (1, None)
                          height: root.width
                      GridLayout:
                          cols: 2
                          # just add a id that can be accessed later on
                          id: content
                  
                  <Root>:
                      content: sb.content
                      Button:
                          center_x: root.center_x
                          text: 'press to add_widgets'
                          size_hint: .2, .2
                          on_press:
                              sb.content.clear_widgets()
                              root.load_content()
                      SideBar:
                          id: sb
                          size_hint: .2, 1
                          image: 'data/images/image-loading.gif'
                  ''')
                  
                  class Root(FloatLayout):
                  
                      content = ObjectProperty(None)
                      '''This is initialised to None and in kv code at line 28
                      above (the one with `content: sb.content`) a ref to the
                      actual content is passed'''
                  
                      def load_content(self):
                          content = self.content
                          for but in range(20):
                              content.add_widget(Button(
                                                  text=str(but)))
                  
                  class MyApp(App):
                      def build(self):
                          return Root()
                  
                  if __name__ == '__main__':
                          MyApp().run()
                  

                  这篇关于具有动态网格布局的 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中更改按钮或标签文本颜色)
                    <i id='5pjws'><tr id='5pjws'><dt id='5pjws'><q id='5pjws'><span id='5pjws'><b id='5pjws'><form id='5pjws'><ins id='5pjws'></ins><ul id='5pjws'></ul><sub id='5pjws'></sub></form><legend id='5pjws'></legend><bdo id='5pjws'><pre id='5pjws'><center id='5pjws'></center></pre></bdo></b><th id='5pjws'></th></span></q></dt></tr></i><div id='5pjws'><tfoot id='5pjws'></tfoot><dl id='5pjws'><fieldset id='5pjws'></fieldset></dl></div>

                      1. <tfoot id='5pjws'></tfoot>

                        <small id='5pjws'></small><noframes id='5pjws'>

                            <tbody id='5pjws'></tbody>
                          <legend id='5pjws'><style id='5pjws'><dir id='5pjws'><q id='5pjws'></q></dir></style></legend>
                          • <bdo id='5pjws'></bdo><ul id='5pjws'></ul>