I recently started using kivy, and I have a question about the change of the background. I need to create multiple widgets with different backgrounds. I'm doing it wrong, but I did not get:
Kv file: <[email protected]>: canvas.before: Color: rgb: self.rgb Rectangle: pos: self.pos size: self.size main.py widget = Widget() myWidget1 = Factory.MyWidget(rgb=[1,0,0]) myWidget2 = Factory.MyWidget(rgb=[0,1,0]) myWidget3 = Factory.MyWidget(rgb=[0,0,1]) widget.add_widget(myWidget1) widget.add_widget(myWidget2) widget.add_widget(myWidget3)
How can I make it better?
As the comments above say, the problem is that MyWidget doesn't have an 'rgb' attribute (nor does a BoxLayout).
In the python file, you need to create the class with that attribute. Specifically, you should create a ListProperty attribute so that all the automatic binding on the kv side happens.
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
rgb = ListProperty([0,0,0])