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.
An example:
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()
This will return:
Parent of ScreenOne: <__main__.ScreenManagement object at 0x7f98a3fddb40>
Parent of ScreenTwo: None
FloatLayout to the rescue pos_hint will make the values relative to the size/position of the parent. So here, for b1 x will be the x of the parent, and center_y will be at the middle between y and top .
Widgets in Kivy are organized in trees. Your application has a root widget , which usually has children that can have children of their own. Children of a widget are represented as the children attribute, a Kivy ListProperty .
The KV language, sometimes called kvlang or the kivy language, allows you to create your widget tree in a declarative way and to bind widget properties to each other or to callbacks in a natural manner.
self refers to current widget instance Rectangle. Rectangle is not a widget, it is a canvas instruction. self does refer to the current widget, which in your example is the PongGame .
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()
It's just not available in their __init__
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With