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

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

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

      1. Kivy:在python中添加的小部件中获取父级

        时间:2023-10-08
        1. <i id='LDRNz'><tr id='LDRNz'><dt id='LDRNz'><q id='LDRNz'><span id='LDRNz'><b id='LDRNz'><form id='LDRNz'><ins id='LDRNz'></ins><ul id='LDRNz'></ul><sub id='LDRNz'></sub></form><legend id='LDRNz'></legend><bdo id='LDRNz'><pre id='LDRNz'><center id='LDRNz'></center></pre></bdo></b><th id='LDRNz'></th></span></q></dt></tr></i><div id='LDRNz'><tfoot id='LDRNz'></tfoot><dl id='LDRNz'><fieldset id='LDRNz'></fieldset></dl></div>

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

                  <tbody id='LDRNz'></tbody>
                  <bdo id='LDRNz'></bdo><ul id='LDRNz'></ul>
                  <tfoot id='LDRNz'></tfoot>
                  <legend id='LDRNz'><style id='LDRNz'><dir id='LDRNz'><q id='LDRNz'></q></dir></style></legend>

                • 本文介绍了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中添加的小部件中获取父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

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

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

                • <legend id='QLO6o'><style id='QLO6o'><dir id='QLO6o'><q id='QLO6o'></q></dir></style></legend>
                    <bdo id='QLO6o'></bdo><ul id='QLO6o'></ul>
                    <tfoot id='QLO6o'></tfoot>