python - kivy中的动态转换图像
问题描述
每次单击按钮时,我都想更改 kv 文件中的图像。问题是我有一个图像,每次单击按钮时都会改变,但图像保持不变
class RandomWindow(Screen,Widget):
img_src = StringProperty('Alcohol-Abuse.jpg')
def randomDrink(self):
...
r = requests.get(photo,stream=True, headers={'User-agent': 'Mozilla/5.0'})
img_src = StringProperty('img.jpg')
if r.status_code == 200:
with open("img.jpg", 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
------ kv.file---
Label:
pos_hint:{"x":0.05, "y":0.35}
size_hint: 0.45,0.5
canvas:
Rectangle:
pos: self.pos
size: self.size
source: root.img_src
解决方案
这有效:
class RandomWindow(Screen,Widget):
def randomDrink(self):
....
r = requests.get(photo,stream=True, headers={'User-agent': 'Mozilla/5.0'})
if r.status_code == 200:
with open("img.jpg", 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
self.ids.im.reload()
---kv.file----
Image:
id: im
source:'img.jpg'
推荐阅读
- vba - 如何在 Excel VBA 的 for 循环中从数组中调用命名范围?
- css - 匹配模式 css - 不工作
- c++ - Qt 动作不会连接到插槽
- java - Maven 阴影插件替换 persistence.xml
- javascript - 单击模态同意按钮时未选中复选框
- php - 如何检查字符串是否不存在然后返回 false?
- apache-kafka - 如何使用 kafka-python 获取 kafka 主题的分区?
- c - 当我尝试使用 struct 运行程序时出现分段错误(核心转储)
- c# - C# Compile OutOfMemory 错误,而我正在尝试动态生成要编译的文件操作代码
- clojure - Clojure - 使用 Cloud Firestore 获取实时更新