#!/usr/bin/env python
# -*- coding: latin-1 -*-
#
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.clock import Clock
kv_string = '''
<DisplayDailyTemperatureForecast>:
GridLayout:
rows: 1
size: min(root.size[0], "500 dp"), "100 dp"
spacing: ["5 dp", 0]
Label:
text: "Temperatur"
size_hint_x: None
width: "100 dp"
Label:
text: str(root.forecast.hourly[0]["temp"])
Label:
text: str(root.forecast.hourly[1]["temp"])
Label:
text: str(root.forecast.hourly[2]["temp"])
Label:
text: str(root.forecast.hourly[3]["temp"])
Label:
text: str(root.forecast.hourly[4]["temp"])
Label:
text: str(root.forecast.hourly[5]["temp"])
'''
Builder.load_string(kv_string)
class TemperatureForecast(object):
"""Retrieve data from weather forecast service and store it internally.
Data format:
.hourly
[0 .. ]
"hour" (time of forecast)
"temp" (temperature)
"pop" (probabilitiy of precipitation)
"""
def __init__(self, **kwargs):
"""Create instance variables. """
self.hourly = []
self.target_hours = [6, 9, 12, 15, 18, 21]
def initData(self):
# define time points where we want information. if the lists are still empty, initialize them to a list of silly numbers:
while len(self.hourly) < len(self.target_hours):
self.hourly.append(None)
self.hourly[len(self.hourly) - 1] = {
"hour": self.target_hours[len(self.hourly) - 1],
"temp": -99,
"pop": -98 }
class DisplayDailyTemperatureForecast(Widget):
""" Retrieve forecast and display it in a gridlayout """
forecast = TemperatureForecast()
forecast.initData()
forecast.hourly[0]["temp"] = 1968 # works
def updateForecast(self, dt):
print "old value is: ", self.forecast.hourly[0]["temp"]
self.forecast.hourly[0]["temp"] = 2015 # does NOT change displayed number...
print "new value is: ", self.forecast.hourly[0]["temp"]
class DisplayApp(App):
def build(self):
self.theWeather = DisplayDailyTemperatureForecast()
Clock.schedule_once(self.theWeather.updateForecast, 4)
return self.theWeather
if __name__ == '__main__':
DisplayApp().run()
Label:
text: str(root.forecast.hourly[0]["temp"])
updateForecast()
methode of the display class. It manipulates the data of the 'forecast' variable. But this is not reflected in the GridViews's display! Why, isn't it bound??
Label:
text: root.text_label
class DisplayDailyTemperatureForecast(Widget):
text_label = StringProperty("temp")2. In your main updateForecast. update the text label value. This will automatically update your label.def updateForecast(self, dt):
self.text_label = str(self.forecast.hourly[0]["temp"])
forecast
.hourly
[0 .. ]
"hour" (time of forecast)
"temp" (temperature)
"pop" (probabilitiy of precipitation)
.daily
[0 .. ]
"day" (day of forecast)
"temp_low"
"temp_high"
"pop" (probabilitiy of precipitation)
"conditions" (human readable description)
forecast_hourly_0_hour = NumericProperty(forecast.hourly[0]["hour"])
forecast_
hourly_1_hour = NumericProperty(forecast.hourly[1]["hour"])
forecast_
hourly_2_hour = NumericProperty(forecast.hourly[2]["hour"])
forecast_
hourly_3_hour = NumericProperty(forecast.hourly[3]["hour"])
forecast_
hourly_4_hour = NumericProperty(forecast.hourly[4]["hour"])
forecast_
hourly_5_hour = NumericProperty(forecast.hourly[5]["hour"])
forecast_
hourly_0_temp = NumericProperty(forecast.hourly[0]["temp"])
forecast_
hourly_1_temp = NumericProperty(forecast.hourly[1]["temp"])
forecast_
hourly_2_temp = NumericProperty(forecast.hourly[2]["temp"])
forecast_
hourly_3_temp = NumericProperty(forecast.hourly[3]["temp"])
forecast_
hourly_4_temp = NumericProperty(forecast.hourly[4]["temp"])
forecast_
hourly_5_temp = NumericProperty(forecast.hourly[5]["temp"])
forecast_
hourly_0_pop = NumericProperty(forecast.hourly[0]["pop"])
forecast_
hourly_1_pop = NumericProperty(forecast.hourly[1]["pop"])
forecast_
hourly_2_pop = NumericProperty(forecast.hourly[2]["pop"])
forecast_
hourly_3_pop = NumericProperty(forecast.hourly[3]["pop"])
forecast_
hourly_4_pop = NumericProperty(forecast.hourly[4]["pop"])
forecast_
hourly_5_pop = NumericProperty(forecast.hourly[5]["pop"])
In my opinion, this destroys one of kivy's biggest advantages, the direct binding of a grafical element to its underlying data: the real data is in the structure, the displayed data is in the kivy properties.
JSON is a widly used data format in the web and Python's support of it is incredible. But then you get a big data structure. And how to get this data structure properly displayed without bulldozing it completely into single NumericProperties?
#!/usr/bin/env python
# -*- coding: latin-1 -*-
#
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.clock import Clock
kv_string =
'''
<DisplayDailyTemperatureForecast>:
GridLayout:
rows: 1
size: min(root.size[0], "500 dp"), "100 dp"
spacing: ["5 dp", 0]
Label:
text: "Temperatur"
size_hint_x: None
width: "100 dp"
Label:
id: temp0
text: str(root.forecast.hourly[0]["temp"])
Label:
text: str(root.forecast.hourly[1]["temp"])
Label:
text: str(root.forecast.hourly[2]["temp"])
Label:
text: str(root.forecast.hourly[3]["temp"])
Label:
text: str(root.forecast.hourly[4]["temp"])
Label:
text: str(root.forecast.hourly[5]["temp"])
'''
Builder.load_string(kv_string)
class TemperatureForecast(object):
"""Retrieve data from weather forecast service and store it internally.
Data format:
.hourly
[0 .. ]
"hour" (time of forecast)
"temp" (temperature)
"pop" (probabilitiy of precipitation)
"""
def __init__(self, **kwargs):
"""Create instance variables. """
self.hourly = []
self.target_hours = [6, 9, 12, 15, 18, 21]
def initData(self):
# define time points where we want information. if the lists are still empty, initialize them to a list of silly numbers:
while len(self.hourly) < len(self.target_hours):
self.hourly.append(None)
self.hourly[len(self.hourly) - 1] = {
"hour": self.target_hours[len(self.hourly) - 1],
"temp": -99,
"pop": -98 }
class DisplayDailyTemperatureForecast(Widget):
""" Retrieve forecast and display it in a gridlayout """
forecast = TemperatureForecast()
forecast.initData()
forecast.hourly[0]["temp"] = 1968 # works
def updateForecast(self, dt):
print "old value is: ", self.forecast.hourly[0]["temp"]
self.forecast.hourly[0]["temp"] = 2015 # does NOT change displayed number...
self.ids['temp0'].text = str(2015)
(in the kv:)
Label:
id: label_1
Label:
id: label_2
...
(and in the code:)
self.ids['label_0'].text =
str(root.forecast.hourly[0]["temp"])
self.ids['label_1].text =
str(root.forecast.hourly[1]["temp"])
...
def updateDisplay(self):
self.ids['label_forecast_hourly_0_temp'].text = str(self.forecast.hourly[0]["temp"])
self.ids['label_forecast_hourly_1_temp'].text = str(self.forecast.hourly[1]["temp"])
self.ids['label_forecast_hourly_2_temp'].text = str(self.forecast.hourly[2]["temp"])
self.ids['label_forecast_hourly_3_temp'].text = str(self.forecast.hourly[3]["temp"])
self.ids['label_forecast_hourly_4_temp'].text = str(self.forecast.hourly[4]["temp"])
self.ids['label_forecast_hourly_5_temp'].text = str(self.forecast.hourly[5]["temp"])
self.ids['label_forecast_hourly_0_pop'].text = str(self.forecast.hourly[0]["pop"])
self.ids['label_forecast_hourly_1_pop'].text = str(self.forecast.hourly[1]["pop"])
self.ids['label_forecast_hourly_2_pop'].text = str(self.forecast.hourly[2]["pop"])
self.ids['label_forecast_hourly_3_pop'].text = str(self.forecast.hourly[3]["pop"])
self.ids['label_forecast_hourly_4_pop'].text = str(self.forecast.hourly[4]["pop"])
self.ids['label_forecast_hourly_5_pop'].text = str(self.forecast.hourly[5]["pop"])
for x in range(0, 6):
self.ids['label_forecast_hourly_{0}_temp'.format(x)].text = str(self.forecast.hourly[x]["temp"])
self.ids['label_forecast_hourly_{0}_pop'.format(x)].text = str(self.forecast.hourly[x]["pop"])
def updateDisplay(self):
for i in range(0, len(self.forecast.hourly)):
self.ids['label_forecast_hourly_{}_temp'.format(i)].text = str(self.forecast.hourly[i]["temp"])
self.ids['label_forecast_hourly_{}_pop'.format(i)].text = str(self.forecast.hourly[i]["pop"])