surpavan
surpavan

Reputation: 1422

Animating in float layout KIVY

I am new to python and kivy. I am trying a small game, I got struck in the starting itself, I want to use float layout, and make widgets fall from top to bottom in it. The widgets size is (0.1,0.1), the animation of movement would take pixes as option, how can I know the max limits of x and y corner pixels in the layout, I tried to know the pixels count of float layout by using the dp(value) from Module: kivy.metrics, however it is showing only as 0,0.

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.metrics import dp

class Test(FloatLayout):
    def __init__(self, **kwargs):
        super(Test,self).__init__(**kwargs)
        print(dp(self.size))


class game(App):
    def build(self):
        return Test()

if __name__ == '__main__':
    game().run()

Code is just an example on how I tried to get dp. How can I move a widget without knowing the max pixels possible.

Upvotes: 1

Views: 1097

Answers (1)

famousgarkin
famousgarkin

Reputation: 14126

The default Widget.size of a layout widget seems to be (1, 1) when initialized, if you don't specify it manually, but at the widget creation time (i.e. the Widget.__init__) you cannot get any other size value from it.

After the widget is created and returned from the App.build method it automatically gets added to the widget tree of the application as a root widget right under the window.

When widget gets added to a parent it also gets resized according to it's size hint value relative to it's parent size. The default Widget.size_hint value is also (1, 1), which makes it scale to the parent size, the window size in this case.

Only after this happens can you get the final size of the widget, e.g. in the App.on_start method that gets called after the App.build:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout

class Test(FloatLayout):
    def __init__(self, **kwargs):
        super(Test,self).__init__(**kwargs)
        print(self.parent)
        # None
        print(self.get_parent_window())
        # None
        print(self.get_root_window())
        # None
        print(self.size)
        # [1, 1]
        print(self.size_hint)
        # [1, 1]

class game(App):
    def build(self):
        print(self.root)
        # None
        return Test()

    def on_start(self):
        print(self.root)
        # <__main__.Test object at 0x10a9d8a10>
        print(self.root.parent)
        # <kivy.core.window.window_pygame.WindowPygame object at 0x10a90d6d0>
        print(self.root.get_parent_window())
        # <kivy.core.window.window_pygame.WindowPygame object at 0x10a90d6d0>
        print(self.root.get_root_window())
        # <kivy.core.window.window_pygame.WindowPygame object at 0x10a90d6d0>
        print(self.root.size)
        # [800, 600]

if __name__ == '__main__':
    game().run()

All of the application and widget lifecycle and behavior is described in the Kivy docs, but it's rather spread all over the place between guides and API reference. Definitely don't miss out the Widget Programming Guide.

Upvotes: 1

Related Questions