What is the size_hint_x attribute in Kivy for?

Asked

Viewed 230 times

0

During my studies in the Kivy framework, I discovered two attributes of the object BoxLayout called size_hint_xand size_hint_y that by what they say, they fit the size of a widget to the size of the screen (it even makes sense because its translation would be "size tip").

Soon I thought that size_hint_x and size_hint_y received only the values True and False to turn them on or off. But seeing some code on the internet, I came across numerical values being passed as argument. Example:

widget.size_hint_x = 0.3
widget.size_hint_y = 5

So now goes the title of this Question: What exactly are the attributes size_hint and what its usefulness ?

2 answers

1

size_hint_x is the width a child widget has relative to the width available in the parent widget.

Let’s say your code has a Boxlayout and you want to put two buttons on it, the second button will be twice the width of the first button:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button


class Aplicativo(App):
    def build(self):
        caixa = BoxLayout()
        botao_0 = Button(text='BOTAO_0', size_hint_x=1)
        botao_1 = Button(text='BOTAO_1', size_hint_x=2)

        caixa.add_widget(botao_0)
        caixa.add_widget(botao_1)

        return caixa


Aplicativo().run()

Captura de tela com código executado em uma janela mostrando 2 botões, um ao lado do outro, sendo que o da direita tem aproximadamente o dobro da largura do primeiro

If you want to determine a fixed width, specific for the second button (Boxlayout child widget), regardless of the available width of the parent, you have to turn off this property by typing size_hint_x = None (None is Python null) and then, for example, write width = 100

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button


class Aplicativo(App):
    def build(self):
        caixa = BoxLayout()
        botao_0 = Button(text='BOTAO_0', size_hint_x=1)
        botao_1 = Button(text='BOTAO_1', size_hint_x=None, width=100)

        caixa.add_widget(botao_0)
        caixa.add_widget(botao_1)

        return caixa


Aplicativo().run()

Captura de tela apresentando uma janela com 2 botões, sendo que o da direita é muito mais estreito que o da esquerda.

Note that the code of the first button has not changed.

I used Python 3.7, Kivy 1.11.1 and Pycharm Community 2020.2

1

According to the documentation:

size_hint is a Referencelistproperty for the properties (size_hint_x, size_hint_y).

That is, the size_hint receives a tuple in which it will be assigned to size_hint_x and size_hint_y, following this form:

size_hint = ReferenceListProperty(size_hint_x, size_hint_y)

In the source there is this description:

The attribute size_hint is a tuple of values used by the layout to manage the size of your children, it indicates the relative layout size instead of the absolute size in pixels, points, cm, etc.

See the definition at the source.

  • Yes I know about it. What I want to know is what exactly does the (size_hint,x,size_hint_y) in the application. What are these values ? It is possible to explain this in more detail ?

  • You asked about size_hint, he’s just the reference to size_hint_x and size_hint_y to set the widget size relative to the child.

  • All right, I’ll improve the question all right ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.