为了解决这个问题,需要对kv和py文件进行以下增强。在
方法1-Kivy自动创建并添加了一个ObjectProperty rgbaIf the widget doesn’t have a property with the given name, an
ObjectProperty will be automatically created and added to the widget.
kv文件添加一个类属性rgba,并将其初始化为默认颜色,(1, .2, .2, .2)到类规则:
将标签颜色替换为root.rgba
代码片段-kv文件:
rgba: (1,.2,.2,.2) # Kivy auto created & added ObjectProperty, "rgba"
...
BoxLayout:
Label:
size_hint_x: .8
id: label
canvas.before:
Color:
rgba: root.rgba
...
py文件删除方法Do_Task()中的所有代码
添加self.rgba = [.5, 1, .2, 1],其中{}引用当前小部件,即EachTask对象。在
Snippets-py文件
^{pr2}$
方法2-显式声明rgba
kv文件将rgba: (1,.2,.2,.2)替换为root.rgba
代码片段-kv文件:
...
BoxLayout:
Label:
size_hint_x: .8
id: label
canvas.before:
Color:
rgba: root.rgba
...
py文件添加import语句,from kivy.properties import ListProperty
声明ListProperty类型的类属性rgba,并将其初始化为默认颜色,[1, .2, .2, .2]在类EachTask()中
删除方法Do_Task()中的所有代码
添加self.rgba = [.5, 1, .2, 1],其中{}引用当前小部件,即EachTask对象。在
Snippets-py文件from kivy.properties import ListProperty
...
class EachTask(BoxLayout):
rgba = ListProperty([1, .2, .2, .2])
...
def Do_Task(self, instance):
self.rgba = [.5, 1, .2, 1]