Home > database >  Why does the kv file return different value of the py file?
Why does the kv file return different value of the py file?

Time:07-13

In my kv file a button size receives its texture size, when I print the size with the kv file it returns the real size but when I print with the py file it returns a different value. Why?

It's my kv file code, it prints [87, 25]:

<Base_P_Brick>:
    background_color: 0, 0, 0, 0
    markup: True
    size_hint: (None, None)
    size: (self.texture_size[0], 25)
    on_press:
        self.abrir()
        print(self.size)

It's my py file code, it prints [0, 25]:

class Base_P_Brick(Button):
    def __init__(self, palavra, **kw):
        super().__init__(**kw)
        self.text = palavra.replace('\n', '')
        self.font_size = '20sp'
        print(self.size)

CodePudding user response:

In an __init__() method, the size of a widget has not yet been assigned, so your print is just printing the default initial size of any widget (which is probably (100,100)).

You can add a method to your Base_P_Brick class that will report the size whenever it changes. Something like this:

class Base_P_Brick(Button):
    def on_size(self, instance, new_size):
        print('on_size:', instance, new_size)
  • Related