I'm having trouble changing the color of a simple widget in Kivy. I can set the color when I create the widget, but I can't change it afterwards.
Here is the simple layout definition file test.kv
. It defines a circle where the color (actually just the r, from rgba), position and size are all linked to variables in the widget class.
<CircleWidget>: canvas.before: Color: rgb: self.rgb Rectangle: pos: self.pos size: self.size
def __init__(s, **kwargs): s.size= [50,50] s.pos = [100,50] s.rgb = [1,0,0] super(CircleWidget, s).__init__(**kwargs)
def on_touch_down(s, touch): if s.collide_point(touch.x,touch.y): s.pos = [s.pos[1],s.pos[0]] # This works s.rgb = [0,0,1] # <---- This does nothing!
class TestApp(App): def build(s): parent = Widget() w = CircleWidget() parent.add_widget(w) return parent
if __name__ == '__main__': TestApp().run()
Can anyone see the problem?
class CircleWidget(Widget):
rgb = ListProperty([1., 1., 1.)]
def __init__(s, **kwargs):
s.size= [50,50]
s.pos = [100,50]
s.rgb = [1,0,0]
super(CircleWidget, s).__init__(**kwargs)
def on_touch_down(s, touch):
if s.collide_point(touch.x,touch.y):
s.pos = [s.pos[1],s.pos[0]] # This works
s.rgb = [0,0,1]