我有一個我用Python/Kivy編寫的天氣應用程序,它在應用程序啓動時成功下載並加載圖像,但天氣圖每天都在變化,而且不會令人耳目一新。 Kivy文檔討論了nocache: True
,image.reload()
&我試過self.the_24h_chart.source.reload()
和兩個錯誤。用Python刷新Kivy圖像
如何重新加載圖像文件?僞代碼:
- 下載新的圖像,並覆蓋原有的同名
- 重新加載圖像
我目前Kivy代碼:
#! /usr/bin/env python
# -*- decoding: utf-8 -*-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, StringProperty, ListProperty
from kivy.clock import Clock
from kivy.loader import Loader
import time
from datetime import datetime, timedelta
import socket
import urllib
#--------------- kivy code ------------------
Builder.load_string('''
<Chart1>:
name: 'chart1'
the_24h_chart: _chart_img
Image:
source: 'images/chart.jpg'
nocache: True
id: _chart_img
size_hint_y: None
height: dp(420)
''')
#-------------------------------------------
class Chart1(Screen):
def update_chart1(self, sec):
try:
urllib.urlretrieve('http://wxcharts.eu/charts/gfs/uk/06/overview_030.jpg', 'images/chart.jpg')
self.the_24h_chart.source = 'images/chart.jpg'
self.Image.reload()
except:
print 'Error Updating Image'
pass
class ScreenManagerApp(App):
def build(self):
sm = ScreenManager()
self.chart1_screen = Chart1(name = 'chart1')
sm.add_widget(self.chart1_screen)
return sm
def on_start(self):
event1 = Clock.create_trigger(self.chart1_screen.update_chart1)
Clock.schedule_interval(self.chart1_screen.update_chart1, 3600) # 1 Hour
event1()
#===========================================================
# run the App !
ScreenManagerApp().run()
如上所述,這聽起來像是'Image.reload'中的一個錯誤。你能提供一個最小的可運行示例來測試嗎? – inclement
我編輯了原始文章,現在工作示例與重新加載圖像錯誤 – northwarks