[PYTHON] About Kivy root

About app and root often used in Kivy

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

Where and when is root?

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

About Kivy root
About Kivy settings screen
About LangID
About CAGR
About python-apt
About Permission
[Python Kivy] About changing the design theme
About requirements.txt
About locale
kivy introduction
About permissions
About Opencv ②
About axis = 0, axis = 1
About Opencv ③
About import
About numpy
About pip
About Linux
About numpy.newaxis
About endian
About Linux
Kivy installation
About import
About Opencv ①
About Linux
About Linux
About Linux ①
About cv2.imread
About _ and __
About wxPython
About left justification and right justification of Kivy Label