Skip to main content
 首页 » 编程设计

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

2024年10月17日8luoye11

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

一个例子:

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 添加的小部件实际上具有对其父级的有效引用:

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__ 方法中不可用。