简体   繁体   中英

Change Label Color

Help please i'm trying to use objectproperty to change the color of the label. The function works because it prints out in the console. The label color or text doesnt change though and i'm not sure what i'm doing wrong.

<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

Try using the init function to create the indicator and activate objects:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM