试图在他的书用 Kivy 创建应用程序中查看天气应用程序。我目前遇到一个我不知道如何解决的错误。
我得到以下信息error
:
city, country = data_item ValueError: 太多值无法解压
感谢任何帮助解决此错误
蟒蛇代码:
from kivy.app import App
from kivy.network.urlrequest import UrlRequest
import json
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, ListProperty
from kivy.uix.listview import ListItemButton
from kivy.factory import Factory
class LocationButton(ListItemButton):
location = ListProperty()
class AddLocationForm(BoxLayout):
search_results = ObjectProperty()
search_input = ObjectProperty()
def args_converter(self, index, data_item):
city, country = data_item
return {'location':(city, country)}
def search_location(self):
search_template = "http://api.openweathermap.org/data/2.5/find?q={}&type=like&APPID=" + "a3ce4c4c978cae4ace6c3e404899ac32"
search_url = search_template.format(self.search_input.text)
request = UrlRequest(search_url, self.found_location)
#TODO : Error Checking if location is not found
def found_location(self, request, data):
data = json.loads(data.decode()) if not isinstance(data, dict) else data
cities = ["{}({})".format(d['name'], d['sys']
['country']) for d in data['list']]
self.search_results.item_strings = cities
del self.search_results.adapter.data[:]
self.search_results.adapter.data.extend(cities)
self.search_results._trigger_reset_populate()
#TODO: Error checking if network failure
class WeatherRoot(BoxLayout):
current_weather = ObjectProperty()
def show_current_weather(self, location):
self.clear_widgets()
if location is None and self.current_weather is None:
location = ("New York (US)", "US")
if location is not None:
self.current_weather = Factory.CurrentWeather()
self.current_weather.location = location
self.add_widget(self.current_weather)
def show_add_location_form(self):
self.clear_widgets()
self.add_widget(AddLocationForm())
class WeatherApp(App):
pass
if __name__ == '__main__':
WeatherApp().run()
基维文件:
#: import main weather
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
WeatherRoot:
<WeatherRoot>:
AddLocationForm:
<AddLocationForm>:
orientation: 'vertical'
search_input: search_box
search_results: search_results_list
BoxLayout:
height: '40dp'
size_hint_y: None
TextInput:
id: search_box
size_hint_x: 50
focus: True
multiline: False
on_text_validate: root.search_location()
Button:
text: 'Search'
size_hint_x: 25
on_press: root.search_location()
Button:
text: 'Current Location'
size_hint_x: 25
ListView:
id: search_results_list
adapter:
ListAdapter (data = [], cls= main.LocationButton,
args_converter = root.args_converter)
Button:
text: 'Cancel'
size_hint_y: None
height: '40dp'
on_press: app.root.show_current_weather(None)
<LocationButton>:
text: "{} ({})".format(self.location[0], self.location[1])
height: "40dp"
size_hint_y: None
on_press: app.root.show_current_weather(self.location)
<CurrentWeather@BoxLayout>:
location: ['New York', 'US']
conditions: None
temp: None
temp_min: None
temp_max: None
orientation: "vertical"
Label:
text: "{} ({})".format(root.location [0], root.location[1])
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: '40dp'
Button:
text: 'Add Location'
on_press: app.root.show_add_location_form()
Button:
text: "Forecast"