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

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

        • <bdo id='JGHRF'></bdo><ul id='JGHRF'></ul>
      1. <legend id='JGHRF'><style id='JGHRF'><dir id='JGHRF'><q id='JGHRF'></q></dir></style></legend>

        <tfoot id='JGHRF'></tfoot>
      2. Kivy:在python中添加的小部件中获取父级

        时间:2023-06-06

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

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

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

                  <tbody id='Lc1Sd'></tbody>
                  本文介绍了Kivy:在python中添加的小部件中获取父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在不是由 kvlang 添加但在 python 中添加的小部件中获取对父级的引用.通常你只需调用 self.parent 但是如果在 python 中将小部件添加到父级,则返回 Null.

                  How do I get the reference to a parent inside a widget that is not added by kvlang but in python. Normally you would just call self.parent however that returns Null if the widget is added in python to the parent.

                  一个例子:

                  import kivy
                  kivy.require('1.9.0') # replace with your current kivy version !
                  
                  from kivy.app import App
                  from kivy.lang import Builder
                  from kivy.uix.screenmanager import ScreenManager,Screen
                  from kivy.clock import Clock
                  
                  kvlang = '''
                  <ScreenManagement>:
                      ScreenOne:
                  
                  <ScreenOne>:
                      name: 'First'
                  
                  
                  <ScreenTwo>:
                      name: 'Second'
                  
                  '''
                  
                  
                  class ScreenManagement(ScreenManager):
                      def __init__(self,**kwargs):
                          super().__init__(**kwargs)
                  
                          def setup(*args):
                              self.add_widget(ScreenTwo())    #add ScreenTwo later in python
                  
                          Clock.schedule_once(setup)
                  
                  class ScreenOne(Screen):
                      def __init__(self,**kwargs):
                          super().__init__()
                  
                          def setup(*args):
                              print("Parent of ScreenOne: {}".format(self.parent))        #this is working
                          Clock.schedule_once(setup)
                  
                  class ScreenTwo(Screen):
                      def __init__(self,**kwargs):
                          super().__init__()
                  
                          def setup(*args):
                              print("Parent of ScreenTwo: {}".format(self.parent))        #this is not working, self.parent will return None
                          Clock.schedule_once(setup)
                  
                  
                  
                  class MyApp(App):
                  
                      def build(self):
                          Builder.load_string(kvlang)
                          return ScreenManagement()
                  
                  
                  if __name__ == '__main__':
                      MyApp().run()
                  

                  这将返回:

                  Parent of ScreenOne: <__main__.ScreenManagement object at 0x7f98a3fddb40>
                  Parent of ScreenTwo: None
                  

                  推荐答案

                  使用 add_widget 添加的小部件实际上具有对其父级的有效引用:

                  Widgets added with add_widget actually has a valid reference to their parent:

                  from kivy.app import App
                  from kivy.uix.screenmanager import ScreenManager, Screen
                  
                  from kivy.lang import Builder
                  Builder.load_string('''
                  <ScreenTwo>
                      Label:
                          text: 'Hello, world'    
                  ''')
                  
                  class ScreenManagement(ScreenManager):
                      def __init__(self,**kwargs):
                          super(ScreenManagement, self).__init__(**kwargs)
                          screen = ScreenTwo()
                  
                          print(screen.parent)
                          self.add_widget(screen)  
                          print(screen.parent)
                  
                  
                  class ScreenTwo(Screen):
                      def on_touch_down(self, *args):
                          print(self.parent)
                  
                  
                  class MyApp(App):
                      def build(self):
                          return ScreenManagement()
                  
                  
                  if __name__ == '__main__':
                      MyApp().run()
                  

                  它只是在他们的 __init__ 方法中不可用.

                  It's just not available in their __init__ method.

                  这篇关于Kivy:在python中添加的小部件中获取父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有 Kivy 的多台显示器 下一篇:小部件在 GridLayout 中的位置

                  相关文章

                1. <legend id='RRoiB'><style id='RRoiB'><dir id='RRoiB'><q id='RRoiB'></q></dir></style></legend>

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

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

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