我对此很怀疑了一段时间了,一直找不到解决的办法。
所以我用杜斯蒂菲利普斯的“在Kivy中创建应用程序”来学习Python和Kivy。这是一个简单的天气应用程序,当我尝试从openweathermap.com获取数据时,UrlRequest函数不能正常工作。我对kivy和python等方面非常陌生,但正如我所见,函数必须使用两个参数调用"found_location“方法:请求和结果(从url获取的列表)。如果我从浏览器中访问url,就会得到正确的结果,但返回到python时,“结果”没有出现。
下面是带有一些调试打印的代码:
from kivy.app import App
#kivy.require("1.9.1")
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
class AddLocationForm(BoxLayout):
search_input = ObjectProperty()
search_results = ObjectProperty()
def search_location(self):
search_template = "api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=" + "{}"
search_url = search_template.format(self.search_input.text)
print search_url
request = UrlRequest(search_url, self.found_location)
print request
print "Result: ", request.result
def found_location(self, request, data):
print request
print 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']]
print cities
self.search_results.item_strings = cities
print "DONE"
class WeatherApp(App):
pass
if __name__ == '__main__':
WeatherApp().run()在这里控制台:
[INFO ] [OSC ] using <multiprocessing> for socket
[INFO ] [Base ] Start application main loop
[INFO ] [GL ] NPOT texture support is available
api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=London
<UrlRequest(Thread-1, started daemon 139654193755904)>
Result: None如您所见,它传递的是正确的URL,在浏览器中我得到了正确的结果,但是从来没有调用"found_location“方法,在python中,request.results = None
我做错什么了?
希望你们能理解我的问题。谢谢你的帮助,为英国人感到抱歉。
发布于 2016-07-20 09:22:41
这里的问题是在成功下载结果之前打印结果。
还记得把"http://“前的链接字符串。
请记住,url是异步加载的。就像UrlRequest上的文档里说的
您可以使用UrlRequest在web上发出异步请求,并在请求完成后获得结果。其精神与Javascript中的XHR对象相同。
这就是为什么要在on_success中使用UrlRequest参数的原因。
我会为你树立一个榜样。
from kivy.app import App
#kivy.require("1.9.1")
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
class MyWidget(BoxLayout):
def __init__(self,**kwargs):
super(MyWidget,self).__init__(**kwargs)
search_url = "http://api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=new%20york"
print search_url
self.request = UrlRequest(search_url, self.res)
print self.request
print "Result: before success", self.request.result,"\n"
def res(self,*args):
print "Result: after success", self.request.result
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()发布于 2021-11-08 05:07:28
#好吧,伙计们,这是最令人震惊的
从kivy.network.urlrequest导入UrlRequest从kivy.clock导入时钟类控制器(Object):
def asynchronous_api_request(self, url):
'''
Method asynchronous_api_request is coded using Kivy urlrequest.py.
'''
Clock.start_clock() # Start the Kivy clock
req = UrlRequest(url) # get request thread
while not req.is_finished: # start while loop to test is_finished
Clock.tick() # tick clock per cycle
Clock.stop_clock() # Stop the clock
return req.result # Return the results如果将 ==命名为“main”:
ctl = Controller()
data = ctl.asynchronous_api_request('https://www.google.com')
print(data)https://stackoverflow.com/questions/38471829
复制相似问题