I was learning how to create an App with Kivy and I see that the App looks different when I use Python vs kv language.
What I am trying to do is to get the background colored red. When I do it with Python, I get a little red box at the bottom left corner (probably, the default 100px kivy widget size). When I do it with the kv file, I get the entire background colored red
Here is my python code -
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import *
class MyWidget(Widget):
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
with self.canvas:
Color(1,0,0,1)
Rectangle(size = self.size, pos = self.pos)
class MyWidgetApp(App):
def build(self):
x = MyWidget()
return x
pass
if __name__ == "__main__":
MyWidgetApp().run()
Here is my kv file
<MyWidget@Widget>:
canvas:
Color:
rgba: 1, 0, 0, 1.0
Rectangle:
size: self.size
pos: self.pos
Why does this happen? And how can I set the background color from Python? I am using Python 2.7 on a Linux machine
1 Answer 1
The problem is that kv automatically makes a binding to update the red rectangle position and size, while python doesn't (and can't). You have to use the bind method to create your own binding. I have a blog post about this here - coincidentally, it addresses precisely your example.