更改标签颜色

时间:2020-03-25 18:14:43

标签: python kivy

请帮助我尝试使用objectproperty更改标签的颜色。该功能起作用是因为它会在控制台中打印出来。标签颜色或文字不会改变,但我不确定自己在做什么。

<Box3@BoxLayout>:
    GridLayout:

        indicator: my_indicator
        cols: 1
        size: root.width/3, root.height/2

        Label:
            id: my_indicator
            pos: self.pos
            text: 'test'
            canvas.before:
                Color:
                    rgba: (1.0, 0.0, 0.0, 1.0)
                Rectangle:
                    pos: self.pos
                    size: self.size
class main_kv(GridLayout):

    indicator = ObjectProperty(Widget)
    activate = ObjectProperty(None)

    def changeColour(self):

        self.indicator.color = 1,0,1,1
        self.indicator.text = 'changed'
        print('button clicked')

    pass

        Button:
            id: activate
            text: 'Arm'
            on_press: app.root.changeColour()
            background_color: (0.4, 0.7, 0.8, 1.0)
            pos: self.parent.center
            font_size: 40
            opacity: 0.8

1 个答案:

答案 0 :(得分:0)

尝试使用init函数创建指标并激活对象:

class main_kv(GridLayout):

    def __init__(self):
        self.indicator = ObjectProperty(Widget)
        self.activate = ObjectProperty(None)

    def changeColour(self):

        self.indicator.color = 1,0,1,1
        self.indicator.text = 'changed'
        print('button clicked')

    pass