victorcd
victorcd

Reputation: 439

How to change the image source in kivy?

I am trying to make an app that shows one image. When I click in some button, the function will change the source of this image, therefore this image will change. So, I tried only change the line image.source, I think the correct way to do this is using a stringproperty, but I also have not had success with it. So, my code is: .py

class PrimeiroScreen(Screen):
    def __init__(self, **kwargs):
        self.name = 'um'
        super(Screen,self).__init__(**kwargs)
        fonte = StringProperty('cafe.jpg')

    def fc1(self):
        self.ids.image.source = (fonte)
        self.ids.image.fonte = "cafe.jpg"
        #self.ids.image.source = "cafe.jpg"


class MyImage(Image):
    fonte = StringProperty()

class RootScreen(ScreenManager):
    pass

and .kv:

<RootScreen>:
    PrimeiroScreen:

<PrimeiroScreen>:
    StackLayout:
        orientation: 'tb-rl'
        ScrollView:
            #size: 100, 100
            #pos_hint: {'center_x': .5, 'center_y': .5}
            GridLayout:
                cols: 1
                padding: 10, 10
                spacing: 5, 5
                size_hint: (None, None)
                size_hint_y:  None
                width: 500
                height: self.minimum_height
                Button:
                    text: "1"
                    #color: 0,0,0,1
                    font_size: '20dp'
                    size: (200, 40)
                    size_hint: (None, None)
                    on_release: root.fc1()
    AnchorLayout:
    Image:
        source:'logo.jpg'
        id: image

I am learning about AnchorLayout, and StackLayout, so it's not working. Very thanks everyone, for awser my newbie question. Nice weekend.

Upvotes: 4

Views: 4561

Answers (1)

Peter Badida
Peter Badida

Reputation: 12199

The thing is with your current code there is no fonte defined(at least for me with copy&paste), you need to use self.fonte.

But anyway, you should access the source directly, otherwise you'd need to make another function that handles your property updating and doing something when it changes i.e. passing it to source, which already is a StringProperty, therefore you'd go really around the solution until you reached it.

<RootScreen>:
    PrimeiroScreen:

<PrimeiroScreen>:
    StackLayout:
        orientation: 'tb-rl'
        ScrollView:
            #size: 100, 100
            #pos_hint: {'center_x': .5, 'center_y': .5}
            GridLayout:
                cols: 1
                padding: 10, 10
                spacing: 5, 5
                size_hint: (None, None)
                size_hint_y:  None
                width: 500
                height: self.minimum_height
                Button:
                    text: "1"
                    color: 0,0,0,1
                    font_size: '20dp'
                    size: (200, 40)
                    size_hint: (None, None)
                    on_release: image.source='newcafe.png'
    MyImage:
        source: 'cafe.jpg'
        id: image

Upvotes: 2

Related Questions