gpt4 book ai didi

Python/Kivy - UrlRequest 结果

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:28 24 4
gpt4 key购买 nike

我为此苦苦挣扎了一段时间,但找不到解决方案。

所以我通过 Dusty Phillips 的“在 Kivy 中创建应用程序”学习 Python 和 Kivy。这是一个简单的天气应用程序,当我尝试从 openweathermap.com 获取数据时,UrlRequest 函数无法正常工作。我对 kivy 和 python 很陌生,但正如我所见,该函数必须使用两个参数调用“found_location”方法:请求和结果(从 url 获得的列表)。如果我从浏览器访问 url,我会得到正确的结果,但返回 python,“结果”为 NONE。

这是带有一些用于调试的打印的代码:

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

我做错了什么?

希望你们能理解我的问题。感谢您的帮助,对英语感到抱歉。

最佳答案

这里的问题是你在下载成功之前打印结果。

还要记得在链接字符串前加上“http://”。

请记住,url 是异步加载的。正如 UrlRequest 上的文档中所说

You can use the UrlRequest to make asynchronous requests on the web and get the result when the request is completed. The spirit is the same as the XHR object in Javascript.

这就是为什么要在 UrlRequest 中使用 on_success 参数

我给你举个例子

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()

关于Python/Kivy - UrlRequest 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38471829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com