用python编写一个fx_用 Python 编写一个天气查询应用

本文介绍了如何使用Python通过API获取天气信息并展示在GUI界面中。主要步骤包括:通过requests库获取天气API数据,解析JSON格式的天气信息,使用Qt Designer创建UI界面,将ui文件转换为py文件,连接信号与槽函数,最后运行主窗口类展示查询结果。
摘要由CSDN通过智能技术生成

效果预览:

一、获取天气信息 使用python获取天气有两种方式。

1)是通过爬虫的方式获取天气预报网站的HTML页面,然后使用xpath或者bs4解析HTML界面的内容。 2)另一种方式是根据天气预报网站提供的API,直接获取结构化数据,省去了解析HTML页面的步骤。 本例使用的是第二种方式,请求地址为:http://wthrcdn.etouch.cn/weather_mini?citykey=城市代码部分城市代码对应:北京 101010100 天津 101030100 上海 101020100 浏览器返回的天津气温情况如下,该信息其实就是一个JSON字符串,格式化之后的样子如下所示: { "data": { "yesterday": { "date": "1日星期五", "high": "高温 17℃", "fx": "东北风", "low": "低温 8℃", "fl": "", "type": "多云" }, "city": "北京", "forecast": [ { "date": "2日星期六", "high": "高温 14℃", "fengli": "", "low": "低温 8℃", "fengxiang": "北风", "type": "小雨" }, ], "ganmao": "昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。", "wendu": "12" }, "status": 1000, "desc": "OK" } 获取天气的主要代码如下: # cityCode 替换为具体某一个城市的对应编号# 1、发送请求,获取数据 url = f‘http://wthrcdn.etouch.cn/weather_mini?citykey={cityCode}‘ res = requests.get(url) res.encoding = ‘utf-8‘res_json = res.json() # 2、数据格式化 data = res_json[‘data‘] city = f"城市:{data[‘city‘]} " # 字符串格式化的一种方式 f"{}" 通过字典传递值 today = data[‘forecast‘][0] date = f"日期:{today[‘date‘]} " # 换行 now = f"实时温度:{data[‘wendu‘]}度 " temperature = f"温度:{today[‘high‘]} {today[‘low‘]} " fengxiang = f"风向:{today[‘fengxiang‘]} " type = f"天气:{today[‘type‘]} " tips = f"贴士:{data[‘ganmao‘]} " result = city + date + now + temperature + fengxiang + type + tips print(result) 二、界面的实现 1、使用Qt Designer绘制窗口,保存为ui文件

2、把ui文件转为py文件 1)在生成的ui文件目录下,打开cmd2)输入以下命令(注意替换名称) pyuic5 -o destination.py source.ui 3、信号与槽函数的连接 # 1、清空按钮与对应函数连接 clearBtn.clicked.connect(widget.clearResult) # 2、查询按钮与对应函数连接 queryBtn.clicked.connect(widget.queryWeather) 4、调用主窗口类 import sys from PyQt5.QtWidgets import QApplication , QMainWindow from WeatherWin import Ui_widget import requests import json class MainWindow(QMainWindow ): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.ui = Ui_widget() self.ui.setupUi(self) # 通过文本框传入想要搜索的城市名称:天津 cityName = self.ui.weatherComboBox.currentText() # 获取天气部分省略 # 在文本框显示查询结果 self.ui.resultText.setText(result) def clearResult(self): print(‘* clearResult ‘) self.ui.resultText.clear() if __name__=="__main__": app = QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_()) 以上,提供了获取天气(GUI)程序的主要过程及部分源码。1、获取天气信息 2、绘制可视化界面 3、把ui文件转成py文件 4、信号与槽 5、调用主窗口类

效果预览:

一、获取天气信息

使用python获取天气有两种方式。

1)是通过爬虫的方式获取天气预报网站的HTML页面,然后使用xpath或者bs4解析HTML界面的内容。

2)另一种方式是根据天气预报网站提供的API,直接获取结构化数据,省去了解析HTML页面的步骤。

本例使用的是第二种方式,请求地址为:http://wthrcdn.etouch.cn/weather_mini?citykey=城市代码

部分城市代码对应:

北京 101010100

天津 101030100

上海 101020100

浏览器返回的天津气温情况如下,该信息其实就是一个JSON字符串,格式化之后的样子如下所示:

{

"data": {

"yesterday": {

"date": "1日星期五",

"high": "高温 17℃",

"fx": "东北风",

"low": "低温 8℃",

"fl": "",

"type": "多云"

},

"city": "北京",

"forecast": [

{

"date": "2日星期六",

"high": "高温 14℃",

"fengli": "",

"low": "低温 8℃",

"fengxiang": "北风",

"type": "小雨"

},

],

"ganmao": "昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。",

"wendu": "12"

},

"status": 1000,

"desc": "OK"

}

获取天气的主要代码如下:

# cityCode 替换为具体某一个城市的对应编号# 1、发送请求,获取数据url =f‘http://wthrcdn.etouch.cn/weather_mini?citykey={cityCode}‘res = requests.get(url)

res.encoding =‘utf-8‘res_json = res.json()# 2、数据格式化data = res_json[‘data‘]

city =f"城市:{data[‘city‘]}

"# 字符串格式化的一种方式 f"{}" 通过字典传递值today = data[‘forecast‘][0]

date =f"日期:{today[‘date‘]}

"#

换行now =f"实时温度:{data[‘wendu‘]}度

"temperature =f"温度:{today[‘high‘]} {today[‘low‘]}

"fengxiang =f"风向:{today[‘fengxiang‘]}

"type =f"天气:{today[‘type‘]}

"tips =f"贴士:{data[‘ganmao‘]}

"result = city + date + now + temperature + fengxiang + type + tips

print(result)

二、界面的实现

1、使用Qt Designer绘制窗口,保存为ui文件

2、把ui文件转为py文件

1)在生成的ui文件目录下,打开cmd2)输入以下命令(注意替换名称)

pyuic5 -o destination.py source.ui

3、信号与槽函数的连接

# 1、清空按钮与对应函数连接

clearBtn.clicked.connect(widget.clearResult)

# 2、查询按钮与对应函数连接

queryBtn.clicked.connect(widget.queryWeather)

4、调用主窗口类

import sys

from PyQt5.QtWidgets import QApplication , QMainWindow

from WeatherWin import Ui_widget

import requests

import json

class MainWindow(QMainWindow ):

def __init__(self, parent=None):

super(MainWindow, self).__init__(parent)

self.ui = Ui_widget()

self.ui.setupUi(self)

# 通过文本框传入想要搜索的城市名称:天津

cityName = self.ui.weatherComboBox.currentText()

# 获取天气部分省略

# 在文本框显示查询结果

self.ui.resultText.setText(result)

def clearResult(self):

print(‘* clearResult  ‘)

self.ui.resultText.clear()

if __name__=="__main__":

app = QApplication(sys.argv)

win = MainWindow()

win.show()

sys.exit(app.exec_())

以上,提供了获取天气(GUI)程序的主要过程及部分源码。

1、获取天气信息

2、绘制可视化界面

3、把ui文件转成py文件

4、信号与槽

5、调用主窗口类

原文:https://www.cnblogs.com/7758520lzy/p/12145294.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值