Carl Von
Carl Von

Reputation: 178

Kivy Image / Remove Image

Regarding a Kivy Image widget. I can't seem to figure out how to remove the source to make it blank.

For example :

kivy_image_widget.source = None

I would like to clear the contents of an image widget when there is no data to display.

Thanks, Dan

Upvotes: 1

Views: 2166

Answers (2)

ikolim
ikolim

Reputation: 16041

Solution

Since source is a StringProperty, assign an empty string to it.

kivy_image_widget.source = ''

Image »source

source

Filename / source of your image.

source is a StringProperty and defaults to None.

Example

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout


class RootWidget(BoxLayout):

    def clear_image(self):
        self.ids.img2.source = ''
        self.ids.img2.reload()


class TestApp(App):

    def build(self):
        return RootWidget()


if __name__ == "__main__":
    TestApp().run()

test.kv

#:kivy 1.11.0

<RootWidget>:
    orientation: 'vertical'

    Image:
        id: img1
        source: 'kivyLogo.png'

    Image:
        id: img2
        source: 'raspberrypi.png'

    Button:
        text: 'Clear Image 2'
        size_hint_y: 0.1
        on_release:
            root.clear_image()

Output

Img01 Img02 - Image 2 Cleared

Upvotes: 2

rpi_guru
rpi_guru

Reputation: 309

If you just need to make it blank, use opacity property like this:

kivy_image_widget.opacity = 0

Upvotes: 0

Related Questions