How to dynamically change an image in a kivy label

Asked

Viewed 177 times

0

I want to dynamically change an image on a kivy label. every time the variable increases a number the label should change the image in python

def mudaimagem(self):
    erros = self.qtd_erros
    if erros == 1:
        #Deve ser escrito aqui
        self.ids.box1.image = "erro1.png"

.Kv

   StackLayout:
        Label:
            id: box1
            size_hint: .4,1
            font_size: '30sp'
            multiline: True
            color: 0,0,0,1
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size

1 answer

0


You can create a new Imagelabel class with the image property.

file . py:

class StackLayout(StackLayout):
    pass

class ImageLabel(Label):
    image = StringProperty()

class StackApp(App):
    def build(self):
        self.root = StackLayout()
        self.qtd_erros = 1
        self.mudaimagem()
        return self.root

    def mudaimagem(self):
        erros = self.qtd_erros
        if erros == 1:
            #Deve ser escrito aqui
            self.root.ids["box1"].image = "erro1.png"

file . Kv:

<StackLayout>:
    ImageLabel:
        id: box1
        size_hint: .4,1
        font_size: '30sp'
        multiline: True
        color: 0,0,0,1
        canvas.before:
            Rectangle:
                pos: self.pos
                size: self.size
                source: self.image

Browser other questions tagged

You are not signed in. Login or sign up in order to post.