positioning widget in a box layout

275 views
Skip to first unread message

Glen Rubin

unread,
Jan 2, 2023, 8:48:51 PM1/2/23
to Kivy users support
I am trying to position widgets inside a box layout, so for example if i have the following in a kv file:

<BoxLayout>:
    Label:
        text:'hi there'

If i add the pos property or pos_hint property below the text it doesn't seem to do much.  First off, the text is centered no matter what pos or pos_hint properties i assign.  I am only able to use pos_hint to change the vertical position of the text, moving the text up with a value between 0 and .5, and moving the text down with a similar negative value.  How would I say move the text to be left aligned?

thanks for any advice!

Elliot Garbus

unread,
Jan 3, 2023, 10:01:42 AM1/3/23
to kivy-...@googlegroups.com

There are a few things going on here.

The Label is filling the layout.  By default the size_hint is one, meaning the Label widget will fill the layout.  When experimenting with Layouts you may find it easier to use a Button, the button shows you how big the widget it.

 

Layouts are tools for sizing and positioning widgets.  The BoxLayout controlling the horizontal position, you can use the position hint to change the position of the widget.  This is NOT what you want to do to change the position of the text.

 

You can change the position of the text inside the Label widget.  See: https://kivy.org/doc/stable/api-kivy.uix.label.html?highlight=label#text-alignment-and-wrapping

Use the technique described to left align the text in the widget.

 

As an alternative you could replace the BoxLayout with a RelativeLayout, set the size of the Label to the texture_size, and use pos_hint to position the Label.

--
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/ec121ce5-f26a-4c45-a244-05b01a88bd22n%40googlegroups.com.

 

Glen Rubin

unread,
Jan 4, 2023, 7:38:14 PM1/4/23
to Kivy users support
Thank you Eliot for clarifying! So texture_size doesn't work with BoxLayout? 

It says in the documentation you linked that texture_size should change the size of the label to the text content, but when I tried doing it had no effect in the BoxLayout.

Glen Rubin

unread,
Jan 4, 2023, 7:41:59 PM1/4/23
to Kivy users support
NVM, I forgot to state size_hint: None, None before using texture_size.  now it works!

Elliot Garbus

unread,
Jan 4, 2023, 7:45:51 PM1/4/23
to kivy-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages