Bradley Ray
Bradley Ray

Reputation: 3

Using kivy, why are the rectangles not changing colors?

I am using the second edition of Kivy-Interactive Applications and Games in Python. The rectangles are supposed to be grey, but they only are white. I downloaded some code from the book off git hub that is supposed to return diagonal red lines, but its white also. Any help would be appreciated, I might just be missing something simple.

# File name: color.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder

Builder.load_string("""
<GridLayout>:
    cols: 2
    Label:
        color: 0.5, 0.5, 0.5, 1
        canvas:
            Rectangle:
                pos: self.x + 10, self.y + 10
                size: self.width - 20, self.height - 20
    Widget:
        canvas:
            Rectangle:
                pos: self.x + 10, self.y + 10
                size: self.width - 20, self.height  - 20
""")

class LabelApp(App):
    def build(self):
        return GridLayout()

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

Upvotes: 0

Views: 248

Answers (1)

el3ien
el3ien

Reputation: 5405

There are a couple of things here.
Your color shall be capitalized, like this Color. And must be inside the canvas.
Then it shall contain, rgba forexample.

Try this:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder

Builder.load_string("""
<GridLayout>:
    cols: 2
    Widget:
        canvas:
            Color: 
                rgba: 0.5, 0.5, 0.5, 1
            Rectangle:
                pos: self.x + 10, self.y + 10
                size: self.width - 20, self.height - 20
    Widget:
        canvas:
            Rectangle:
                pos: self.x + 10, self.y + 10
                size: self.width - 20, self.height  - 20
""")

class LabelApp(App):
    def build(self):
        return GridLayout()

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

Upvotes: 2

Related Questions