Fox
Fox

Reputation: 21

KIVY - Changing color of a canvas from Python

After many trial and research I did not manage to accomplish what I want to do.

I have a kivy file (lets say test.kv) in which I have the following section:

BoxLayout:
    id: WebcamSection
    orientation: "vertical"
    canvas:
        Color:
            rgb: (0.87451, 0.294118, 0.266667, 1)
        Rectangle:
            pos: self.pos
            size: self.size
    KivyCamera:
        allow_stretch: True
        keep_ratio: True
        id: pbyCam

From my .py I would like to be able to modify the color of that Canvas to (1,1,1,1) but so far I did not find the solution.

I am currently changing the color for many other elements such as buttons, labels... without any issues

Any suggestion about how I should handle that?

Thanks a lot for you help

Upvotes: 1

Views: 495

Answers (1)

Fox
Fox

Reputation: 21

Ok I finally got the answer after additional trials :)

Here is the change I have made to my .kv file:

BoxLayout:
        id: WebcamSection
        orientation: "vertical"
        test_color: (0.87451, 0.294118, 0.266667, 1)
                canvas:
                    Color:
                        rgb: self.test_color
                    Rectangle:
                        pos: self.pos
                        size: self.size

So in the end it was very easy, just need to add a variable (in this case I called it test_color) and refer to it in order to set the color of the canvas.

Then in my .py I can call the id of the BoxLayout: self.WebcamSection = self.ids['WebcamSection']

And to finish I have a function to change the color with the below line: self.WebcamSection.test_color = (1,1,1,1)

Upvotes: 1

Related Questions