<small id='SQj91'></small><noframes id='SQj91'>

  • <legend id='SQj91'><style id='SQj91'><dir id='SQj91'><q id='SQj91'></q></dir></style></legend>

    1. <tfoot id='SQj91'></tfoot>
      <i id='SQj91'><tr id='SQj91'><dt id='SQj91'><q id='SQj91'><span id='SQj91'><b id='SQj91'><form id='SQj91'><ins id='SQj91'></ins><ul id='SQj91'></ul><sub id='SQj91'></sub></form><legend id='SQj91'></legend><bdo id='SQj91'><pre id='SQj91'><center id='SQj91'></center></pre></bdo></b><th id='SQj91'></th></span></q></dt></tr></i><div id='SQj91'><tfoot id='SQj91'></tfoot><dl id='SQj91'><fieldset id='SQj91'></fieldset></dl></div>
        <bdo id='SQj91'></bdo><ul id='SQj91'></ul>

        我如何在我的 kivy 程序中使用 json api

        时间:2023-10-09

        <small id='tqVCP'></small><noframes id='tqVCP'>

            <tbody id='tqVCP'></tbody>
              <bdo id='tqVCP'></bdo><ul id='tqVCP'></ul>
              <i id='tqVCP'><tr id='tqVCP'><dt id='tqVCP'><q id='tqVCP'><span id='tqVCP'><b id='tqVCP'><form id='tqVCP'><ins id='tqVCP'></ins><ul id='tqVCP'></ul><sub id='tqVCP'></sub></form><legend id='tqVCP'></legend><bdo id='tqVCP'><pre id='tqVCP'><center id='tqVCP'></center></pre></bdo></b><th id='tqVCP'></th></span></q></dt></tr></i><div id='tqVCP'><tfoot id='tqVCP'></tfoot><dl id='tqVCP'><fieldset id='tqVCP'></fieldset></dl></div>
            • <tfoot id='tqVCP'></tfoot>

              <legend id='tqVCP'><style id='tqVCP'><dir id='tqVCP'><q id='tqVCP'></q></dir></style></legend>

                • 本文介绍了我如何在我的 kivy 程序中使用 json api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  您好,我目前正在阅读 O'Reilly 的《在 Kivy 中创建应用程序》,有一个示例我无法使其正常工作,因为在他写这本书时 openWeatherMap 不需要 api 密钥 (APPID),但现在它需要,而且我是一名新手程序员,不知道如何更改代码以使其正常工作.

                  hi I'm currently reading O'Reilly's Creating Apps in Kivy and there's an example that I can't get it to work correctly because by the time he wrote the book openWeatherMap didn't require api key (APPID) but now it does and I'm a novice programmer and don't know how to change the code so it would work.

                  这是main.py源代码:

                  this is the main.py source code:

                  from kivy.app import App
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.properties import ObjectProperty
                  from kivy.network.urlrequest import UrlRequest
                  import json
                  
                  class AddLocationForm(BoxLayout):
                      search_input = ObjectProperty()
                  
                  def search_location(self):
                      search_template = "http://api.openweathermap.org/data/2.5" + "find?q={}&type=like"
                      search_url = search_template.format(self.search_input.text)
                      request = UrlRequest(search_url, self.found_location)
                  
                  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
                      print("
                  ".join(cities))
                  
                  class WeatherApp(App):
                  pass
                  
                  if __name__ == '__main__':
                  WeatherApp().run()
                  

                  这是weather.kv源代码:

                  and this is weather.kv source code:

                  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
                      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
                      item_strings: []
                  

                  代码很简单,您在文本框中输入城市名称并点击搜索,它会通过显示收到的名称来确认.

                  the code's simple you put a city name in textbox and hit search and it confirms it by showing the name it recieved.

                  推荐答案

                  好吧,不知道是不是迟到了,但是最近买了这本书,我也发现自己被这个问题卡住了.在谷歌搜索这个问题时,我偶然发现了你的问题以及这本书的 O'Reilly 链接.这就是作者对这个问题的看法:

                  OK, so I don't know if I'm late or not but having bought this book recently, I too found myself stuck exactly in this problem. Upon Googling this issue, I happened to stumble upon your question as well as O'Reilly link for this book. This is what the author had to say about this problem:

                  我已确认问题;openweathermap 已更改他们的查询过程和书中的网址现在都坏了.这将彻底破坏所有新读者的读者体验;我们需要做一个更新,也许应该谈谈第二版."

                  "I've confirmed the issue; openweathermap has changed their query process and the urls in the book are now all broken. This is going to utterly ruin the reader experience for all new readers; we'll need to do an update and should maybe talk about a second edition."

                  幸运的是,一位好心人找到了解决这个问题的方法.但为了做到这一点,您必须首先创建一个免费的开放天气帐户.创建帐户后,您将获得一个 API 密钥.它会在您的个人资料中.

                  Luckily a good Samaritan found out the solution to this problem. But in order to do so you must first create a free Open Weather account. After creating the account, you'll get an API key. It'll be in your profile.

                  所以,现在这段代码:

                  search_template = "http://api.openweathermap.org/data/2.5" + "find?q={}&type=like"
                  

                  变成:

                  search_template = "http://api.openweathermap.org/data/2.5/find?q={}&type=like&APPID=" + "YOUR_API_KEY"
                  

                  这对我有用.我知道我迟到了 3 个月,现在你可能已经得到了答案,但我认为这对遇到类似问题的人很有用,他们的谷歌结果会将他们带到这个地方.

                  This worked for me. I know I'm 3 months late and probably by now you'd have gotten your answer, but I thought this will be useful for those who run into similar problems and their Google result will bring them to this place.

                  这篇关于我如何在我的 kivy 程序中使用 json api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:KivyMD 如何更改 MDToolbar 标题大小和字体? 下一篇:嵌套在 BoxLayout 内的 ScreenManager 不可见

                  相关文章

                  最新文章

                    <i id='jyEsx'><tr id='jyEsx'><dt id='jyEsx'><q id='jyEsx'><span id='jyEsx'><b id='jyEsx'><form id='jyEsx'><ins id='jyEsx'></ins><ul id='jyEsx'></ul><sub id='jyEsx'></sub></form><legend id='jyEsx'></legend><bdo id='jyEsx'><pre id='jyEsx'><center id='jyEsx'></center></pre></bdo></b><th id='jyEsx'></th></span></q></dt></tr></i><div id='jyEsx'><tfoot id='jyEsx'></tfoot><dl id='jyEsx'><fieldset id='jyEsx'></fieldset></dl></div>

                      • <bdo id='jyEsx'></bdo><ul id='jyEsx'></ul>

                      <legend id='jyEsx'><style id='jyEsx'><dir id='jyEsx'><q id='jyEsx'></q></dir></style></legend>

                      <small id='jyEsx'></small><noframes id='jyEsx'>

                      <tfoot id='jyEsx'></tfoot>