user1217458
user1217458

Reputation: 21

how to get the absolute size of Kivy layouts when using size hints

I've been using size hints to size many of my layouts in Kivy. However, whenever size hints are used, I cannot get the layout's absolute size (pixel size) from the size property. I have found that you can only get absolute size from the layout's size property after setting size_hint to None. How can I get the absolute size and use size hints at the same time? Is there a method or something I can call?

I have added a simple example below.

from kivy.lang import Builder
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window

Builder.load_string('''
<MasterLayout>:
    orientation: 'vertical'

    BoxLayout:
        id: layout1
        orientation: 'vertical'
        size_hint: 1.0, 0.2
        canvas:
            Color:
                rgb: 1, 0, 0
            Rectangle:
                size: self.size
                pos: self.pos

    BoxLayout:
        id: layout2
        orientation: 'vertical'
        size_hint: 1.0, 0.8
        canvas:
            Color:
                rgb: 0, 1, 0
            Rectangle:
                size: self.size
                pos: self.pos
''')


class MasterLayout(BoxLayout):

    def __init__(self, **kwargs):
        super(MasterLayout, self).__init__(**kwargs)

        Window.size = [300, 300]
        print self.ids['layout1'].size
        print self.ids['layout1'].size_hint
        print self.ids['layout2'].size
        print self.ids['layout2'].size_hint

class myApp(App):

    def build(self):

        app = MasterLayout()

        print app.ids['layout1'].size
        print app.ids['layout1'].size_hint
        print app.ids['layout2'].size
        print app.ids['layout2'].size_hint

        return app


if __name__=='__main__':

    myApp().run()

this prints out:

[100, 100]
[1, 0.2]
[100, 100]
[1, 0.8]
[100, 100]
[1, 0.2]
[100, 100]
[1, 0.8]

The two size lists I should be getting is [300, 60] for layout1 and [300, 240] for layout2. I need the absolute size of certain layouts in the init block after I have changed the window size. Similar to the simple code above. I could calculate it based on the size hint and the window size, however it gets complicated when I start nesting many layers of layouts that may or may not include padding and spacing between widgets. Thanks again for the help.

Upvotes: 1

Views: 1018

Answers (1)

inclement
inclement

Reputation: 29488

The size property does contain the actual size of the widget, even with a size_hint.

Perhaps you are accessing it before the parent layout has had time to set the size of its children, i.e. after they are instantiated and added but potentially before the next frame. If this is the case, you could crudely Clock.schedule_once a function to check after a 0 interval (which means after the next frame), or more generally it is often the kivy way to bind to a property and update something whenever it changes.

I can give a more direct answer if you give an example of what you are trying.

Upvotes: 1

Related Questions