5

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

asked Nov 13, 2014 at 17:41

1 Answer 1

5

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.

answered Nov 13, 2014 at 18:21
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.