Nirdesh Kumawat
Nirdesh Kumawat

Reputation: 406

How to refresh background image of Popup in kivy?

I am using Python 2.7 and kivy. I run test.py, and after replacing test.png direct from folder, click on press me then new image doesn't show. Can someone tell me how to refresh the image?

test.py

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.lang import Builder


Builder.load_string('''
#:kivy 1.10.0

<abc>:
    background: 'test.png'

''')


class abc(Popup):
    pass


class PopupApp(App):
    title = 'Popup Demo'

    def build(self):
        self._popup = abc()
        return Button(text="press me", on_press=self._popup.open)


PopupApp().run()

Upvotes: 0

Views: 966

Answers (1)

ikolim
ikolim

Reputation: 16031

Use Popup's on_open event to refresh loading of popup's background.

Example

main.py

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.lang import Builder

Builder.load_string('''
#:kivy 1.11.0

<abc>:
    title : "change title color"
    title_color: 1, 0, 0, 1    # red title
    size_hint: None, None
    size: 400, 400

    BoxLayout:
        orientation: "vertical"

        GridLayout:
            cols: 1
            Label:
                bold: True
                text: "make label bold"
                color: 1, 0, 0, 1    # red color text

            Label:
                markup: True
                text: "[b][color=008000]make[/color] label [color=3333ff]bold[/color][/b]"

''')


class abc(Popup):

    def __init__(self, **kwargs):
        super(abc, self).__init__(**kwargs)
        self.i = 0

    def on_open(self):
        if self.i % 2 == 0:
            self.background = 'DSC08518.JPG'
        else:
            self.background = 'yellow.png'
        self.i += 1


class PopupApp(App):
    title = 'Popup Demo'

    def build(self):
        self._popup = abc()
        return Button(text="press me", on_release=self._popup.open)


PopupApp().run()

Output

Img01 Img02

Upvotes: 1

Related Questions