Why is declaring `size_x` and `size_y` different from delcaring both in `size` in kivy?

67 Views Asked by At

Why do these two blocks yield different results in kivy?

size

size: [50,50]

size_x and size_y

size_x: 50
size_y: 50

Example

For example, the following code does not render the same looking app

size

Using just size has more padding around the label

A "Hello World" app where "Hello" has more padding around it than the next image

#!/usr/bin/env python3

from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.app import App

KV = """
StackLayout:
    orientation: 'lr-tb'

    Label:
        text: "Hello"
        size: [50,50]
        size_hint: None, None

    Label:
        text: "World"
        size: self.texture_size
        size_hint: None, None
"""

class MyApp(App):
    def build(self):
        return Builder.load_string( KV )

size_x and size_y

Using both size_x and size_y has less padding around the label

A "Hello World" app where "Hello" has less padding around it than the previous image

#!/usr/bin/env python3

from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.app import App

KV = """
StackLayout:
    orientation: 'lr-tb'

    Label:
        text: "Hello"
        size_x: 50
        size_y: 50
        size_hint: None, None

    Label:
        text: "World"
        size: self.texture_size
        size_hint: None, None
"""

class MyApp(App):
    def build(self):
        return Builder.load_string( KV )

My understanding is that size is merely a python list of [size_x, size_y]. Because of this, I'd expect that declaring them separately would yield the same results.

Why does declaring size as distinct size_x and size_y variables differ from declaring it just once with size?

1

There are 1 best solutions below

0
tito On

size_x and size_y doesn't exist at all in the Kivy's API, and in the Kivy widget attributes.

size is a reference to a list of [width, height]. Theses code are identicals:

size: 100, 100
size_hint: None, None

is equal to:

width: 100
height: 100
size_hint: None, None

Your example have a different behavior because the default size of a unconstrained Widget is 100, 100. So assigning size_x and size_y are meaningless.