Search code examples
pythoncanvaskivykivy-language

KV Language Rectangle self.size Does Not Work


I'm having trouble displaying a properly sized canvas that only consists of a red rectangle, and I want to fill the rectangle up to the "size" of the canvas. The position works, but the size does not, as follows: The size is too big

While I'm expecting something more like this: A much smaller rectangle

Here's my code: my.kv:

<MyWidget>
    pos: 100, 100
    size: 64, 64
    canvas:
        Color:
            rgb: 1, 0, 0
        Rectangle:
            pos: self.pos
            size: self.size

And MyApp.py:

class MyWidget(Widget):
    pass

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

I've tried setting the pos and size properties directly for the rectangle, but then I realized I couldn't manipulate it dynamically afterwards, so that's not what I needed. And I thought that pos and size under <MyWidget> would act as variables that can be accessed through self. Please help!


Solution

  • Set size_hint: None, None as well as just size, otherwise the size hint takes precedence.