pythonkivyhandler

Change image in a BoxLayout when an action occurs in another Box Layout


I have a sort of note-taking app I'm putting together. It has a horizontal BoxLayout which contains an image and another BoxLayout (which happens to be vertical). This embedded BoxLayout contains a TabbedPanel.

I want to change the image in the main BoxLayout whenever the user selects a different tab in the embedded BoxLayout.

I'm not sure what to do in the on_release() method of each TabbedPanelItem in order to get the different images to display.

Currently I have the layout working the way I want up to the point of displaying the image for the first (default) tab.

Here is my code:

In the KV code, I have set the source of the Image to app.leftImage. In the 3 on_release() handlers, I set app.leftImage to the correct image. But that doesn't change what is displayed.

There is probably something simple that I am not understanding, but I am new to Kivy programming, so please answer with sufficient detail for a newbie to grasp.


Solution

  • I had to add an id to both the BoxLayout and the Image. I also added a property to the BoxLayout to hold the image widget id. Then, in the on_release handler for each TabbedPanelItem, I had to pass the property to the handler. In each handler, I set the image source to the correct image.

    In Kivy code:

        BoxLayout:
            id: mainBox
            leftImage: image
            ...
    
            Image:
                id: image
            ...
    
                    TabbedPanelItem:
                        id: tab_ref
                        text: 'Tab1R'
                        on_release: app.showTab1R(r_list, mainBox.leftImage)
    

    In Python code:

        def showTab1R(self, ref_list, image):
            image.source = self.Tab1RImage