• <legend id='n0Xz5'><style id='n0Xz5'><dir id='n0Xz5'><q id='n0Xz5'></q></dir></style></legend>

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

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

        • <bdo id='n0Xz5'></bdo><ul id='n0Xz5'></ul>

      1. 如何取消绑定在 Kivy 语言中自动绑定的属性?

        时间:2023-06-07
        1. <legend id='aVrcr'><style id='aVrcr'><dir id='aVrcr'><q id='aVrcr'></q></dir></style></legend>

              <tbody id='aVrcr'></tbody>
              <bdo id='aVrcr'></bdo><ul id='aVrcr'></ul>

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

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

                  本文介绍了如何取消绑定在 Kivy 语言中自动绑定的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Kivy 语言会自动在属性中创建内部绑定.例如,如果我们将 parent 的位置分配给 child 的位置,那么 child 的位置将自动更新:

                  The Kivy Language automatically creates internal binds in properties. For example, if we assign the position of the parent to the position of the child, then the position of the child is going to be updated automatically:

                  Widget:
                      Label:
                          pos: self.parent.pos
                  

                  在这种情况下,如果我们移动父 Widget,那么子也将移动.如何解除子属性 pos 的绑定?我知道如何解除绑定(属性)[http://kivy.org/docs/api-kivy.uix.widget.html#using-properties] 我绑定自己,但如果我不知道如何解除绑定它所绑定的方法的名称.

                  In this case, if we move the parent Widget, then the child is also going to move. How do I unbind the property pos from the child? I know how to unbind (properties)[http://kivy.org/docs/api-kivy.uix.widget.html#using-properties] that I bind myself but how do I unbind them if I don't know the name of the method it is bound.

                  这是一个小例子来说明我的意思.Button UpGridLayout 移动到顶部,将 Down 移动到 Bottom.Button Center 位于屏幕中间.我的问题是,当我单击 UpDown 时,我的居中按钮不再存在.

                  Here is a small example to show what I mean. The Button Up moves the GridLayout to the top and Down to the Bottom. The Button Center center itself in the middle of the screen. My problem is that when I click Up or Down my Centered button is not anymore.

                  from kivy.app import App
                  from kivy.uix.floatlayout import FloatLayout
                  from kivy.lang import Builder
                  
                  Builder.load_string("""
                  <Example>:
                      GridLayout:
                          id: _box
                          cols: 3
                          size_hint: .7, .3
                          pos_hint: {'center_x': .5}
                          x: 0
                          Widget:
                              Button:
                                  pos: self.parent.pos
                                  size: self.parent.size
                                  on_press: _box.y = 0
                                  text: "Down"
                          Widget:
                              Button:
                                  pos: self.parent.pos
                                  size: self.parent.size
                                  on_press: self.center_y = root.height/2
                                  text: "Out of the Grid"
                          Widget:
                              Button:
                                  pos: self.parent.pos
                                  size: self.parent.size
                                  on_press: _box.top = root.height
                                  text: "Up"
                  """)
                  
                  class Example(FloatLayout):
                      pass
                  
                  
                  class ExampleApp(App):
                      def build(self):
                          return Example()
                  
                  if __name__ == "__main__":
                      ExampleApp().run()
                  

                  <小时>

                  我为什么要这样做?我在 GridLayout 上使用了一个不断更新位置的动画.按钮的正常位置应该在网格布局内,但有时其中一个按钮会飞过屏幕并返回到相同的位置.问题是,当我的网格布局也在移动时,我无法让它们飞行,因为属性已绑定,并且一旦按钮尝试飞行,它就会返回网格.这也意味着有时需要绑定.我想要的是控制绑定和解除绑定.


                  Why do I want to do that in any case? I am using an animations on the GridLayout that constantly updates the position. The normal position of the buttons should be inside the gridlayout but once in a while one of the buttons flies over the screen and come back to the same position. The problem is that I cannot make them fly while my gridlayout is also moving because the property is bound and as soon as the button try to fly it goes back to the grid. That also means that the binding is sometimes desirable. What I want is have control of the bind and unbind.

                  推荐答案

                  评论现在似乎不起作用,所以我会发布这个作为答案.

                  Comments don't seem to be working right now so I'll post this as a answer.

                  1. 您已经有一个 FloatLayout(您的根小部件).用那个代替创建一个新的 FloatLayout.
                  2. 在从网格中删除小部件之前.
                    • 存储它的大小,
                    • 将 size_hint 设置为无、无
                    • 设置 pos_hint 以将小部件定位在中心.

                  这是您的代码,其中包含这些修复::

                  Here's your code with these fixes::

                  from kivy.app import App
                  from kivy.uix.floatlayout import FloatLayout
                  from kivy.lang import Builder
                  
                  Builder.load_string("""
                  <Example>:
                      center_button: _center_button
                      center_widget: _center_widget
                      grid:_grid
                      GridLayout:
                          id: _grid
                          cols: 3
                          size_hint: .7, .3
                          pos_hint: {'center_x': .5}
                          x: 0
                          Widget:
                              Button:
                                  pos: self.parent.pos
                                  size: self.parent.size
                                  on_press: _grid.y = 0
                                  text: "Down"
                          Widget:
                              id: _center_widget
                              Button:
                                  id: _center_button
                                  pos: self.parent.pos
                                  size: self.parent.size
                                  on_press: root.centerize(*args)
                                  text: "Out of the Grid"
                          Widget:
                              Button:
                                  pos: self.parent.pos
                                  size: self.parent.size
                                  on_press: _grid.top = root.height
                                  text: "Up"
                  """)
                  
                  class Example(FloatLayout):
                      def centerize(self, instance):
                          if self.center_button.parent == self.center_widget:
                              _size = self.center_button.size
                              self.center_widget.remove_widget(self.center_button)
                              self.center_button.size_hint = (None, None)
                              self.add_widget(self.center_button)
                              self.center_button.pos_hint = {'center_x': .5, 'center_y':.5}
                          else:
                              self.remove_widget(self.center_button)
                              self.center_button.size_hint = (1, 1)
                              self.center_widget.add_widget(self.center_button)
                              self.center_button.size = self.center_widget.size
                              self.center_button.pos = self.center_widget.pos
                  
                  class ExampleApp(App):
                      def build(self):
                          return Example()
                  
                  if __name__ == "__main__":
                      ExampleApp().run()
                  

                  更新 1:

                  如果出于某种原因您仍然需要解除绑定 kvlang 绑定的属性,您可以使用自省来获得 属性的观察者列表.所以对于你的情况,它会是这样的::

                  If for whatever reason you still need to unbind the properties bound by kvlang you can do so using introspection to get a list of observers for the property. so for your case it would be something like this::

                  observers = self.center_widget.get_property_observers('pos')
                  print('list of observers before unbinding: {}'.format(observers))
                  for observer in observers:
                      self.center_widget.unbind(pos=observer)
                  print('list of observers after unbinding: {}'.format(self.center_widget.get_property_observers('pos')))
                  

                  您需要为此使用最新的主服务器.我应该预先警告您要非常小心,尽管您需要重置在 kvlanguage 中设置的绑定,但是您会失去 kv 语言的优势......只有在您真正了解自己在做什么时才使用它.

                  You would need to use the latest master for this. I should fore-warn you to be extremely careful with this though you'd need to reset the bindings set in kvlanguage, but then you loose the advantage of kv language... Only use this If you really understand what you are doing.

                  这篇关于如何取消绑定在 Kivy 语言中自动绑定的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 buildozer 构建后,kivy 应用程序中的文件路径无效 下一篇:刷新/重新加载 FileChooser

                  相关文章

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

                  <tfoot id='HPVAs'></tfoot>
                    • <bdo id='HPVAs'></bdo><ul id='HPVAs'></ul>

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

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