我尝试制作一个简单的ToDoList程序。有添加,删除和执行按钮。但是我有一些关于标签颜色的错误。当我在滚动视图中单击“ DO IT”按钮时,标签颜色会更改,但是当其中某些操作完成后,单击“删除”按钮时,彩色标签会更改。我确实使用帆布。我该如何解决这个问题?

class Home(Screen):

    def __init__(self,**kwargs):
        super(Home,self).__init__(**kwargs)

    def addWidget(self):
        task_input = self.ids.task_input.text
        newListItem = EachTask(text=task_input ,
        id=str((len(self.ids.add_field.children))) )
        print(newListItem.id)
        self.ids.add_field.add_widget(newListItem)
class EachTask(BoxLayout):
    def __init__(self, text= "", **kwargs):
        super(EachTask,self).__init__(**kwargs)
        self.ids.label.text = text

    def Do_Task(self,instance):
        child = instance.parent.parent
        with self.canvas.before:
            Color(.5,1,.2,1, mode='rgba')
            Rectangle(pos=child.ids.label.pos, size=child.ids.label.size)


kv_file

<FlatButton@ButtonBehavior+Label>:
    font_size: 15


<Home>:
    BoxLayout:
        id: home
        orientation: "vertical"
        spacing: 5
        #space_x: self.size[0]/2
        canvas.before:
            Color:
                rgba: (1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos

    ##########HEADER#######
        BoxLayout:
            id: header
            size_hint_y: None
            height: 50
            canvas.before:
                Color:
                    rgba: (.85,.7,.2,1)
                Rectangle:
                    size: self.size
                    pos: self.pos
            Label:
                text: "TO DO LIST"
                font_size: "20sp"
                bold: True
                size_hint_x: .9
            FlatButton:
                text: "Back"
                size_hint_x: .1
    ####################################
        ScrollView:
            canvas.before:
                Color:
                    rgba: (1,1,.2,.2)
                Rectangle:
                    size: self.size
                    pos: self.pos
            BoxLayout:
                id: add_field
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
                spacing: 2   #Spaces between childs
    #####################################################
        BoxLayout:
            id: input_field
            size_hint_y: None
            height: 80
            TextInput:
                id: task_input
                focus: True
                size_hint_x: .9
                multiline: False
            Button:
                font_size: "40sp"
                size_hint_x: .1
                text: "+"
                on_release: root.addWidget()
                id: button1
                color: 1,0.5,0.5,1
#######################################################
<EachTask>:
    size_hint_y: None
    height: 50
    id: each_task
    BoxLayout:
        Label:
            size_hint_x: .8
            id: label
            canvas.before:
                Color:
                    rgba: (1,.2,.2,.2)
                Rectangle:
                    size: self.size
                    pos: self.pos
        Button:
            size_hint_x: .1
            text: "X"
            on_release: app.root.ids.add_field.remove_widget(root)
        Button:
            size_hint_x: .1
            text: "DO IT"
            on_release: root.Do_Task(self)

最佳答案

为了解决此问题,kv和py文件需要进行以下增强。

方法1-Kivy自动创建并添加了一个ObjectProperty,rgba

Kivy automatically created & added an ObjectProperty


  如果小部件没有具有给定名称的属性,则
  ObjectProperty将自动创建并添加到小部件中。


KV文件


添加一个类属性rgba并将其初始化为默认颜色,将(1, .2, .2, .2)初始化为类规则<EachTask>:
将标签的颜色替换为root.rgba


片段-KV文件

<EachTask>:
    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],其中self引用当前窗口小部件,即EachTask对象。


片段-py文件

def Do_Task(self, instance):
    self.rgba = [.5, 1, .2, 1]


方法2-明确声明rgba

KV文件


rgba: (1,.2,.2,.2)替换为root.rgba


片段-KV文件

<EachTask>:
    ...
    BoxLayout:
        Label:
            size_hint_x: .8
            id: label
            canvas.before:
                Color:
                    rgba: root.rgba
                ...


py文件


添加导入语句,from kivy.properties import ListProperty
声明类属性,类型为rgbaListProperty并将其初始化为默认颜色,即类[1, .2, .2, .2]中的EachTask()
删除方法Do_Task()中的所有代码
添加self.rgba = [.5, 1, .2, 1],其中self引用当前窗口小部件,即EachTask对象。


片段-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]

关于python - 如何在Kivy中动态更改标签背景颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57080830/

10-12 18:16