python - 如何在kivy中刷新Popup的背景图片?
问题描述
我正在使用 Python 2.7 和 kivy。我运行test.py
,test.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()
解决方案
使用 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()
输出
推荐阅读
- python-3.x - 完成 eval_strfrac(s, base),使其返回其浮点值
- mysql - 与 MySQL 服务器在 ' 处失去连接
: ', 系统错误: 10053 已建立的连接被中止 - bash - 如何从 Oracle DBMS_Scheduler 在 Linux 中执行 bash/shell 脚本?
- php - 如何更改laravel显示的数据库内容的日期格式
- git - 防止错误方式 git merge
- reactjs - 盖茨比在graphql查询返回后获取图像路径
- python - 使用 HTML/Django 调用 python 函数
- android - 如果没有 @Inject 构造函数或来自 @Provides- 或 @Produces-annotated 方法,则无法提供获取。在科特林匕首2
- asp.net-mvc - AspNetCore MVC 声明 - IsInRoleAsync 验证声明
- windows - Jenkins Windows 执行命令行 BitVise sexec 失败,最后退出 1000