首页 > 解决方案 > 如何在kivy中刷新Popup的背景图片?

问题描述

我正在使用 Python 2.7 和 kivy。我运行test.pytest.png直接从文件夹替换后,click on press me新图像不显示。谁能告诉我如何刷新图像?

测试.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()

标签: pythonpython-2.7kivy

解决方案


使用 Popup 的on_open事件刷新弹出的背景加载。

例子

主文件

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

输出

图像01 图像02


推荐阅读