JULIUS
JULIUS

Reputation: 121

How can I access canvas color via python

I want to change the background screen color for different cases. How can I access the canvas color via python...

Thanks Julius

I've tried it with this:

        with self.canvas:
            if time == times_list[6 or 7 or 8 or 9 or 10 or 11 or 12]:
                Color(rgb=utils.get_color_from_hex("#d18154ff"))
            if time == times_list[13 or 14 or 15 or 16 or 17]:
                Color(rgb=utils.get_color_from_hex("#00b6fffff"))
            if time == times_list[18 or 19 or 20 or 21]:
                Color(rgb=utils.get_color_from_hex("#b46e46ff"))
            if time == times_list[22 or 23 or 1 or 2 or 3 or 4 or 5 or 6]:
                Color(rgb=utils.get_color_from_hex("#000000a8"))

<DuesseldorfWindow>:
    name: "DueW"
    canvas.before:
        Color:
            rgb : utils.get_color_from_hex("#00b6fffff")
        Rectangle:
            pos: self.pos
            size: self.size

Upvotes: 0

Views: 169

Answers (1)

inclement
inclement

Reputation: 29458

The canvas doesn't have a colour, rather it's a list of instructions to run one by one in order to draw your app. The Color instruction sets an internal context variable to say that if something is drawn then it will take that colour.

With that in mind, your top piece of code is probably just adding Color instructions to the canvas that don't do anything, as they come after everything else that's drawn. Instead you need to change the rgb value of the Color instruction in your second piece of code.

I suggest adding a colour = ListProperty(get_color_from_hex("x00b6ffff")) declaration to your DusseldorfWindow class, then writing rgb: root.colour in the kv. Then later when you want to change the colour you write self.colour = ... to change the colour that's already being used instead of adding a new one.

Upvotes: 1

Related Questions