我的第二个请求不起作用

2024-04-26 00:07:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的基维,并试图学习它,但我被困在这个问题。 如您所见,我可以在AddLocationForm类中调用url,但不能在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()

Tags: fromimportselfformaturlsearchdatadef

热门问题