wxpython应用程序_用wxPython创建GUI应用程序展示NASA图片(第一部分)

Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发。

在成长过程中,我时常认为探索宇宙空间是一件令人兴奋的事情,幻想那些未知的事物是相当有趣的。我也喜欢浏览异世界的照片或者探讨广袤的空间。可是这些和Python有什么关系呢?当然有关啦,美国国家航空航天局(NASA)提供了一套基于web的API,可以用于查询他们的图像库。

您可以在他们的网站阅读相关信息。

NASA官网建议先获取API密钥。当访问网站时,需要填写一个简短的表格。

从技术层面上来说,您无需API密钥即可请求NASA服务。然而,对于免密钥的访问,存在速率限制的问题。当然,即使使用密钥进行访问,也会存在每小时至多1000次请求的默认上限值。如果超过限制,将暂时阻止发送请求。可以联系NASA提高上限值。

有趣的是,文档并未说明免密钥情况下的请求次数上限。

关于域名访问方面,NASA的图像API文档和普通API文档之间存在出入,导致使用时令人困惑。

比如,普通API文档上的URL如下:

但是,图像API文档给出的URL有所不同:

对于本教程而言,将采用后者。

NASA API的使用

当开始使用不太熟悉的API时,最好先阅读其官方文档。另外可以在网上搜索是否存在基于该API的Python包。不过似乎没有NASA库的Python版本,所以需要自己实现。

开始使用前,请阅读NASA的图像API文档。

这个文档不是很长,无需花费大量时间阅读,所以请尽量浏览一遍。

接下来将尝试使用该API。

如下是访问API的部分代码:

如果在调试器中运行此代码,则可以打印返回的JSON。

如下是返回的部分内容:

既然已经了解了JSON的格式,可以尝试解析它。

可以在Python脚本中加入如下代码:

由此可以获取JSON的第一个项。然后可以解析出nasa_id,用于获取与此特定结果关联的所有图像。接下来可以将nasa_id添加到新URL并发出请求。

请求结果如下:

Python代码的最后两行用于从JSON中提取URL。至此我们获取了需要展示的全部数据。

用户界面的设计

您可以通过多种不同的方式设计图像下载应用程序。其中原型模式是最简单且快速的。它的优势在于在实现所有基本需求的基础上,可以结合自己掌握的知识增强原型或者进行创新。

如下是一个尝试创建的模型:

如您所见,应用程序需要具有如下功能:搜索栏

保存搜索结果的部件

选择某个结果时的图像显示方式

图像下载功能

接下来让我们一起学习如何创建这个用户界面吧!英文原文:https://www.blog.pythonlibrary.org/2019/04/18/creating-a-gui-application-for-nasas-api-with-wxpython/

译者:我是昵称耶~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2016出版新书,2016.2.27重新上传,此pdf比我之前(2016.01.05)上传的那个版本更好。 Paperback: 304 pages Publisher: Packt Publishing - ebooks Account (January 6, 2016) Language: English ISBN-10: 1785287737 ISBN-13: 978-1785287732 Key Features This book empowers you to create rich cross-platform graphical user interfaces using Python It helps you develop applications that can be deployed on Windows, OSX, and Linux The recipes in the book involve real-world applications, giving you a first-hand experience of the practical scenarios Book Description wxPython is a GUI toolkit for the Python programming language built on top of the cross-platform wxWidgets GUI libraries. wxPython provides a powerful set of tools that allow you to quickly and efficiently building applications that can run on a variety of different platforms. Since wxWidgets provides a wrapper around each platform's native GUI toolkit, the applications built with wxPython will have a native look and feel wherever they are deployed. This book will provide you with the skills to build highly functional and native looking user interfaces for Python applications on multiple operating system environments. By working through the recipes, you will gain insights into and exposure to creating applications using wxPython. With a wide range of topics covered in the book, there are recipes to get the most basic of beginners started in GUI programming as well as tips to help experienced users get more out of their applications. The recipes will take you from the most basic application constructs all the way through to the deployment of complete applications. What you will learn Create full featured user interfaces Design and develop custom controls Deploy and distribute wxPython applications to Windows, Macintosh OS X, Linux, and other UNIX-like environments Handle and respond to application events Manage and display data using grids Interact with web services from your GUI Use Paint events to draw custom displays Support the display of user interfaces in multiple languages

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值