Reputation: 13
How do i update ellipse color in kivy? i tried this
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle, Color
from kivy.clock import Clock
class Touch(Widget):
def __init__(self, **kwargs):
super(Touch, self).__init__(**kwargs)
self.x=0
with self.canvas:
self.rect= Rectangle(pos= (self.x, 200), size= (3, 30), color=Color(0.9,0.9,0.1,1))
Clock.schedule_interval(self.update, 1/30.)
def update(self, *args):
self.x+=3
self.rect.pos= (self.x, 200)
self.rect.size= (self.x/2, 200)
# self.rect.color= Color(int(self.x/100), 0.9, 0.1, 1)
class My_app(App):
def build(self):
return Touch()
My_app().run()
it work when i try to change pos or size, but if i try to change the color by not commenting this line
# self.rect.color= Color(int(self.x/100), 0.9, 0.1, 1)
it will raise this error
AttributeError: 'kivy.graphics.vertex_instructions.Rectangle' object has no attribute 'color'
if we can't update the color of some rectangle, is there a proper way to update it?
[edit] i got answer from reddit, turn out, when you type Color(r,g,b,a), it is making a new object, so if you assign it for example:
color= Color(r,g,b,a)
and you look inside of that object
dir(color)
it have "rgba" attribute, and when we change that attribute, you change the color to whatever shape object that is created when that color object is still active.
Upvotes: 0
Views: 366
Reputation: 38837
Yes, your info from reddit is correct. Here is how to apply it:
class Touch(Widget):
def __init__(self, **kwargs):
super(Touch, self).__init__(**kwargs)
self.x=0
with self.canvas:
self.color = Color(0.9,0.9,0.1,1)
self.rect= Rectangle(pos= (self.x, 200), size= (3, 30))
Clock.schedule_interval(self.update, 1/30.)
def update(self, *args):
self.x+=3
self.rect.pos= (self.x, 200)
self.rect.size= (self.x/2, 200)
self.color.rgb = [self.x/1000., 0, 0, 1]
I have changed the updated rgba
to [self.x/1000., 0, 0, 1]
just to make it more obvious when it is run.
Upvotes: 1