Kivy often uses variables such as app and root.
For example, use it like below.
main.kv
<LargeImage>:
on_touch_down: app.changeScene() #Call changeeScene of MainApp when Widget is touched
Image:
allow_stretch: True
source: root.path
main.py
class LargeImage(Widget):
def __init__(self, path):
self.path = path
super(LargeImage, self).__init__()
class MainApp(App):
def build(self):
self.root = LargeImage()
return self.root
def changeScene(self, scene, opt = None):
pass
For example, if you have the file below The root of the upper Image is LargeImage, and the root of the lower Image is SmallImage.
main.kv
<LargeImage>:
Image:
source: root.path
<SmallImage>:
Image:
source: root.path
Now suppose you run the code below on the python file side.
main.py
l_img = LargetImage()
l_img.add_widget(SmallImage()) #Add SmallImage to LargeImage
In this case, the root of the Image on the SmallImage side remains SmallImage. Adding under LargeImage does not change root to LargeImage.
If you want to access LargeImage from the SmallImage side, Use root.parent instead of root.
Conversely, when accessing Small Image from the Large Image side It can be accessed with root.children [0].
Recommended Posts