如何在猕猴桃中刷新Popup的背景图像?

时间:2018-09-08 10:02:42

标签: python python-2.7 kivy

我正在使用Python 2.7和kivy。我运行test.py,并且直接从文件夹test.png替换了click on press me后,新图像就不会显示。有人可以告诉我如何刷新图像吗?

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()

1 个答案:

答案 0 :(得分:1)

使用弹出式窗口的on_open事件刷新弹出式窗口背景的加载。

示例

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()

输出

Img01 Img02