本文介紹了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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!