我是Kivy的新手并试图学习它,但我遇到了这个问题。
如您所见,我可以在AddLocationForm
课程中调用网址,但无法在CurrentWeather
课程中调用。确切地说,不调用函数weather_retrive
。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty,StringProperty,NumericProperty,ListProperty
from kivy.network.urlrequest import UrlRequest
from kivy.uix.listview import ListItemButton
import json
# from kivy.factory import Factory
class WeatherRoot(BoxLayout):
current_weather= ObjectProperty()
def show_current_weather(self,location=None):
self.clear_widgets()
print "hello {}".format(location)
print "killer {}".format(self.current_weather)
if self.current_weather is None:
self.current_weather= CurrentWeather()
if location is not None:
self.current_weather = CurrentWeather(location=location)
self.current_weather.update_weather()
self.add_widget(self.current_weather)
def show_add_location_form(self):
self.clear_widgets()
self.add_widget(AddLocationForm())
class AddLocationForm(BoxLayout):
search_input = ObjectProperty()
def search_location(self,**kwargs):
search_template = "http://api.apixu.com/v1/search.json?key=310544670f5e490aadb121033173006&q={}"
search_textwithnospace = self.search_input.text.replace(" ","_") # coverting GUI text input "space " to " underscore " format for API to understand
search_url = search_template.format(search_textwithnospace)
print search_url
self.request = UrlRequest(search_url, self.found_location)
# print "first before",self.request.result
# print "The user searched for {}". format(self.search_input.text)
def found_location(self,request,data):
# print "after",self.request.result
cities = ["{}".format(d['name'])for d in data]
it = iter(cities)
tucity = zip(it)
print "\n ".join(cities)
self.search_results.item_strings = tucity
del self.search_results.adapter.data[:]
self.search_results.adapter.data.extend(tucity)
self.search_results._trigger_reset_populate()
def args_converter(self,index,data_item):
city= data_item
return {'location':(city)}
class CurrentWeather(BoxLayout):
location=ListProperty(['Mumbai, India'])
temp=NumericProperty()
def update_weather(self,**kwargs):
weather_template = "http://api.apixu.com/v1/current.json?key=310544670f5e490aadb121033173006&q={}"
string = ''.join(self.location)
weather_url=weather_template.format(string)
print "infact {}".format(string)
print weather_url
request2= UrlRequest(weather_url,self.weather_retrive)
# print "before",request2.result
def weather_retrive(self, request2, data2,*args):
print data2
print "after",self.request2.result
self.temp= data2['current']['temp_c']
print self.temp
class LocationButton(ListItemButton):
location = ListProperty()
class citysearchApp(App):
def build(self):
return WeatherRoot()
if __name__ == '__main__':
citysearchApp().run()