illright
illright

Reputation: 4043

Access canvas elements of Kivy widgets

I've drawn a widget background using a RoundedRectangle in a kv file. Now I want to create a different widget and the only difference from this one is the background color. The properties approach (suggested by @inclement) gives me a strange error. For some reason, the property works when I use it in a different place (cursor_color, for example) so I'd expect it to work here. So, the new question is why isn't that property getting recognized?

<Message>:
    BoxLayout:
        colour_property: 0.99, 0.99, 0.99, 1
        canvas:
            Color:
                rgba: 0.8, 0.8, 0.8, 1
            RoundedRectangle:
                pos: root.pos
                size: self.size
            Color:
                rgba: self.colour_property
            RoundedRectangle:
                pos: root.x + 1, root.y + 1
                size: self.width - 2, self.height - 2

        TextInput:
            pos: root.pos
            size: root.size  

And the Python code:

class Message(Widget):
    colour_property = ListProperty([0.99, 0.99, 0.99, 1])
    def __init__(self, **kwargs):
        self.colour_property = ListProperty([0.99, 0.99, 0.99, 1])
        super(Message, self).__init__(**kwargs)

The error occurs when parsing the rgba: self.colour_property line.

Upvotes: 0

Views: 480

Answers (1)

inclement
inclement

Reputation: 29450

Use a property to transfer the value:

BoxLayout:
    colour_property: 1, 1, 1, 1
    canvas:
        Color:
            rgba: 0.8, 0.8, 0.8, 1
        RoundedRectangle:
            pos: root.pos
            size: self.size
        Color:
            rgba: self.colour_property
        RoundedRectangle:
            pos: root.x + 1, root.y + 1
            size: self.width - 2, self.height - 2

    TextInput:
        pos: root.pos
        size: root.size

You might also want/need to declare the property in your python code (i.e. make a class with the property and make a rule for it instead).

Upvotes: 2

Related Questions