Getting the size of a layout

22 views
Skip to first unread message

Frédéric

unread,
Jun 27, 2024, 11:57:50 AM (5 days ago) Jun 27
to Kivy users support
Hello all,

I'm always having troubles with sizes in Kivy.

Now I want to retrieve the size of a FloatLayout (to add dynamically objets in it).
The size I get is always the default one: (100,100)

For this program
-----------
from kivy.app import App
from kivy.lang import Builder

kv = """
BoxLayout:
    orientation: "vertical"
    canvas:
        Color:
            rgb: 1, .7,  1
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text:"Titre"
        color: 0, 0, 0
        size_hint: 1, 0.1
    FloatLayout:
        id: flt
        canvas:
            Color:
                rgb: 0.5, 1, 0.5
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            id: btn
            size_hint: None, None
            size: 95, 95
"""

class main(App):

    def build(self):
        return Builder.load_string(kv)

    def on_start(self, **kwargs):
        print (self.root.ids.flt.size)
        print (self.root.ids.btn.size)

if (__name__ == "__main__"):
    main().run()


--------
I can see the button, which size (95, 95), and the FloatLayout, in green.
The floatLayout is very big comparatively to the button, but the console output is
[100, 100]
[95, 95]

How can I retrieve the good information?

ELLIOT GARBUS

unread,
Jun 27, 2024, 2:22:18 PM (5 days ago) Jun 27
to Kivy users support
The default size for a widget is 100,100.  The sizes will be set a clock tick after on_start.  
I've modified your code to add print out the sizes after one clock tick.
I also added some print statements to the button.

You might be interested in the inspector tool.  You can use it to interactively  examine the widgets and their attributes.
Read:

from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
            on_release:
                print(f'{flt.size=} {btn.size=}')
"""

class Main(App):

    def build(self):
        return Builder.load_string(kv)

    def on_start(self, **kwargs):
        print(f'on_start: {self.root.ids.flt.size=}  {self.root.ids.btn.size=}')
        Clock.schedule_once(self.after_clock)

    def after_clock(self, _):
        print(f'after_clock: {self.root.ids.flt.size=}  {self.root.ids.btn.size=}')

if __name__ == "__main__":
    Main().run()

From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of Frédéric <freder...@gmail.com>
Sent: Thursday, June 27, 2024 8:57 AM
To: Kivy users support <kivy-...@googlegroups.com>
Subject: [kivy-users] Getting the size of a layout
 
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/05c32407-2a79-4836-a9db-276f72e3d702n%40googlegroups.com.

Frédéric

unread,
Jun 28, 2024, 8:03:58 AM (5 days ago) Jun 28
to Kivy users support
Thanks Elliot,

Your answer is perfect, once again!

As it makes more sense to me (I use on_start to refine the UI) , I then will now use on_start like this

    def on_start(self, **kwargs):
        self.on_will_firstlayout()
        Clock.schedule_once(self.on_did_firstlayout, 0)

ELLIOT GARBUS

unread,
Jun 28, 2024, 10:10:50 AM (5 days ago) Jun 28
to Kivy users support
I encourage you to try the inspector.  It is a very powerful tool. 
On the command line: python yourcode.py -m inspector
Then press cntrl-e 
Select inspect on the bar, and touch the widget.  Click on the bar to get the details.   It is a great debugging tool.


Sent: Friday, June 28, 2024 5:03 AM

To: Kivy users support <kivy-...@googlegroups.com>
Subject: Re: [kivy-users] Getting the size of a layout
 
Reply all
Reply to author
Forward
0 new messages