nagylzs
nagylzs

Reputation: 4178

kivy: why cannot I change the color of my rectangle?

Here is a method that is supposed to draw the canvas of a label:

def update_canvas(self):
    c = self.canvas.after
    c.clear()
    with c:
        Color([1, 1, 1, 1])
        Line(points=[self.point_from, self.point_to])
        if self.texture:
            Color([1, 0, 0, 1]) # RED RECTANGLE!
            Rectangle(pos=self.pos, size=self.size)
            Color([1, 1, 1, 1])
            Rectangle(pos=self.pos, texture=self.texture, size=self.texture.size)

The label used as a tooltip which also has a line pointing to the tooltipped item. The above version produces this output:

enter image description here

If I comment out the Rectangle instruction after the RED COLOR line, this is displayed:

enter image description here

Please note that the text's label is rendered twice, because I have also added a Rectangle(texture=self.texture) instruction, but that is for testing only.

The Label itself is added to the window with this simple call:

Windows.add_widget(label)

The question is this: why is my rectangle not red? (Actually I want it to be half transparent.)

Upvotes: 5

Views: 4609

Answers (1)

sp________
sp________

Reputation: 2645

You are passing a list [] to Color(), while Color expects at least 3 parameters rgb, rgba, rgb + mode or rgba + mode

try this:

def update_canvas(self):
    c = self.canvas.after
    c.clear()
    with c:
        Color(1, 1, 1, 1)
        Line(points=[self.point_from, self.point_to])
        if self.texture:
            Color(1, 0, 0, 1) # RED RECTANGLE!
            Rectangle(pos=self.pos, size=self.size)
            Color(1, 1, 1, 1)
            Rectangle(pos=self.pos, texture=self.texture, size=self.texture.size)

If you want a half transparent rectangle change the rgba to 1,0,0,.5

Upvotes: 4

Related Questions