1.开发环境
语言:python3 开发工具:pycharm 工具库: PyQt5?5.12.1 pyqt5-tools 5.11.2.1.3
注:导入上面两个库要先导入sip库
2.简介
介绍:关于气象预警信息问题,采用Python、QT编程方法,借助聚合数据提供的气象信息接口,制作出一个简单的气象预警软件;达到了对各个省份,不同时间段气象的查询功能,实现了对查询结果复制功能和将结果保存到文件中的功能。
主要工作: 1.功能设计和UI界面的设计 2.整个程序函数的书写(功能实现) 3.软件的测试与打包
3.效果图
我已经将程序打包成了可以执行的weather.exe文件,点击即可运行。
点击后运行:
将结果保存到文件中:
?4.开发步骤
4.1 创建项目并配置虚拟环境
4.2 导入依赖库
4.3 配置QT5扩展工具
4.4 设计UI界面
4.5 程序逻辑功能书写
4.6 软件测试与打包
步骤详细说明见博文:使用Python PyQt5实现一个简单的图像识别软件_yue200403的博客-CSDN博客_图像识别开发软件
5.部分代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'weather.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import json
import sys
import requests
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import urllib, urllib.request
import ssl
import base64
# 调用聚合数据api
API_KEY = ' '
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(572, 326)
self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 191, 41))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label.setLineWidth(1)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.comboBox = QtWidgets.QComboBox(self.horizontalLayoutWidget)
self.comboBox.setObjectName("comboBox")
self.horizontalLayout.addWidget(self.comboBox)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(440, 20, 121, 23))
self.pushButton.setObjectName("pushButton")
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(250, 10, 160, 41))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.dateEdit = QtWidgets.QDateEdit(QDate.currentDate())
self.dateEdit.setObjectName("dateEdit")
self.dateEdit.setDisplayFormat("yyyy-MM-dd")
self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-3652))
self.dateEdit.setMaximumDate(QDate.currentDate().addDays(3652))
self.horizontalLayout_2.addWidget(self.dateEdit)
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(10, 60, 551, 211))
#self.label_3.setStyleSheet("background-color: rgb(255, 255, 0);")
self.label_3.setStyleSheet("border-width: 1px;border-style: solid;boder-color: rgb(0,0,0);")
self.label_3.setWordWrap(True)
self.label_3.setObjectName("label_3")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(150, 280, 81, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(350, 280, 111, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "气象预警软件"))
# 设置图标
Form.setWindowIcon(QIcon('./1.ico'))
#获取地区
#self.areas = self.get_area()
#areas = self.areas
#print(len(areas))
self.areas = [{'province_code': '110000', 'province_name': '北京市'}, {'province_code': '120000', 'province_name': '天津市'}, {'province_code': '130000', 'province_name': '河北省'}, {'province_code': '140000', 'province_name': '山西省'}, {'province_code': '150000', 'province_name': '内蒙古自治区'}, {'province_code': '210000', 'province_name': '辽宁省'}, {'province_code': '220000', 'province_name': '吉林省'}, {'province_code': '230000', 'province_name': '黑龙江省'}, {'province_code': '310000', 'province_name': '上海市'}, {'province_code': '320000', 'province_name': '江苏省'}, {'province_code': '330000', 'province_name': '浙江省'}, {'province_code': '340000', 'province_name': '安徽省'}, {'province_code': '350000', 'province_name': '福建省'}, {'province_code': '360000', 'province_name': '江西省'}, {'province_code': '370000', 'province_name': '山东省'}, {'province_code': '410000', 'province_name': '河南省'}, {'province_code': '420000', 'province_name': '湖北省'}, {'province_code': '430000', 'province_name': '湖南省'}, {'province_code': '440000', 'province_name': '广东省'}, {'province_code': '450000', 'province_name': '广西壮族自治区'}, {'province_code': '460000', 'province_name': '海南省'}, {'province_code': '500000', 'province_name': '重庆市'}, {'province_code': '510000', 'province_name': '四川省'}, {'province_code': '520000', 'province_name': '贵州省'}, {'province_code': '530000', 'province_name': '云南省'}, {'province_code': '540000', 'province_name': '西藏自治区'}, {'province_code': '610000', 'province_name': '陕西省'}, {'province_code': '620000', 'province_name': '甘肃省'}, {'province_code': '630000', 'province_name': '青海省'}, {'province_code': '640000', 'province_name': '宁夏回族自治区'}, {'province_code': '650000', 'province_name': '新疆维吾尔自治区'}, {'province_code': '710000', 'province_name': '台湾省'}, {'province_code': '810000', 'province_name': '香港'}, {'province_code': '820000', 'province_name': '澳门'}]
areas = self.areas
if(len(areas)>0):
for item in areas:
#print(item['province_code'])
self.comboBox.addItem(item['province_name'])
else:
#地区信息获取失败
pass
self.label.setText(_translate("Form", "选择地区:"))
self.pushButton.setText(_translate("Form", "获取气象预警信息"))
self.label_2.setText(_translate("Form", "日期:"))
self.label_3.setText(_translate("Form", ""))
self.pushButton_2.setText(_translate("Form", "复制查询结果"))
self.pushButton_3.setText(_translate("Form", "将结果保存为文件"))
#print(self.comboBox.currentText())
# 为获取气象预警信息添加点击事件
self.pushButton.clicked.connect(self.getWeatherInfo)
# 为复制按钮添加点击事件
self.pushButton_2.clicked.connect(self.copyText)
# 复制功能函数
def copyText(self):
clipboard = QApplication.clipboard()
clipboard.setText(self.label_3.text())
# 获取地区 只抓取省份
def get_area(self):
request_url = "https://apis.juhe.cn/fapig/alarm/citys"
request_url = request_url + "?key=" + API_KEY
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url,headers=headers)
if response:
rs = response.json()
print(response.json())
# 读取数据
areas = []
try:
for item in rs['result']:
areas.append({'province_code':item['province_code'],'province_name':item['province_name']})
except Exception:
return []
return areas
# 根据身份名称匹配代码的
def getCodeByName(self,name):
for item in self.areas:
if(item['province_name']==name):
return item['province_code']
return 0
# 获取天气信息 并显示查询结果
def getWeatherInfo(self):
request_url = "http://apis.juhe.cn/fapig/alarm/queryV2"
#省份代码 匹配
province_name = self.comboBox.currentText()
province_code = ''
if province_name != '':
province_code = self.getCodeByName(province_name)
#时间匹配
date = self.dateEdit.text()
print(date)
request_url = request_url + "?province_code=" + province_code + "&date=" + date + "&key=" + API_KEY
print(request_url)
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, headers=headers)
print(response)
if response:
rs = response.json()
print(response.json())
# 读取数据
info = province_name+'预警信息:\n'
try:
if rs['result'] == None:
if rs['reason']=='success':
self.label_3.setText('暂无预警信息!!!')
else:
self.label_3.setText(rs['reason'])
else:
for item in rs['result']:
region = item['city'] + item['district']
info += '地区:{}\n{}\n'.format(region, item['content'])
self.label_3.setText(info)
except Exception:
info = rs['reason']
self.label_3.setText(info)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = Ui_Form()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
需要完整代码私信我即可。
?
?
|