代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from weatherWin import Ui_Form
import requests
import json
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
def initComboxProvince(self):
with open("city.json", 'r') as load_f:
load_dict = json.load(load_f)
for city in load_dict:
# if city["provinceZh"]== "上海":
# print(city)
cunzai = False
for myProvinceIndex in range(self.ui.provinceComboBox.count()):
if city["provinceZh"] == self.ui.provinceComboBox.itemText(myProvinceIndex):
cunzai = True
if not cunzai:
self.ui.provinceComboBox.addItem(city["provinceZh"])
def provinceComboBoxChange(self):
self.ui.cityComboBox.clear()
provinceZh = str(self.ui.provinceComboBox.currentText())
with open("city.json", 'r') as load_f:
load_dict = json.load(load_f)
for city in load_dict:
if provinceZh == city["provinceZh"]:
cunzaiCity = False
for myCityIndex in range(self.ui.cityComboBox.count()):
if city["leaderZh"] == self.ui.cityComboBox.itemText(myCityIndex):
cunzaiCity = True
if not cunzaiCity:
self.ui.cityComboBox.addItem(city["leaderZh"])
def cityComboBoxChange(self):
self.ui.weatherComboBox.clear()
leaderZh = str(self.ui.cityComboBox.currentText())
with open("city.json", 'r') as load_f:
load_dict = json.load(load_f)
for city in load_dict:
if leaderZh == city["leaderZh"]:
cunzaiCity = False
for myCityIndex in range(self.ui.weatherComboBox.count()):
if city["cityZh"] == self.ui.weatherComboBox.itemText(myCityIndex):
cunzaiCity = True
if not cunzaiCity:
self.ui.weatherComboBox.addItem(city["cityZh"])
def queryWeather(self):
print('* queryWeather ')
cityName = self.ui.weatherComboBox.currentText()
#rep = requests.get('http://www.weather.com.cn/data/sk/' + cityCode + '.html')
rep = requests.get('https://www.tianqiapi.com/api/?version=v6&appid=77123885&appsecret=4SnDowj4&city='+cityName)
rep.encoding = 'utf-8'
print(rep.json())
msg1 = '当前日期: %s' % rep.json()['date'] + '\n'
msg1 += '当前星期: %s' % rep.json()['week'] + '\n'
msg1 += '城市名称: %s' % rep.json()['city'] + '\n'
msg1 += '天气情况: %s' % rep.json()['wea'] + '\n'
msg1 += '实时温度: %s' % rep.json()['tem'] + '℃\n'
msg1 += '高 温: %s' % rep.json()['tem1'] + '℃\n'
msg1 += '低 温: %s' % rep.json()['tem2'] + '℃\n'
msg1 += '风 向: %s' % rep.json()['win'] + '\n'
msg1 += '风力等级: %s' % rep.json()['win_speed'] + '\n'
msg1 += '风 速: %s' % rep.json()['win_meter'] + '\n'
msg1 += '湿 度: %s' % rep.json()['humidity'] + '\n'
msg1 += '能见度: %s' % rep.json()['visibility'] + '\n'
msg1 += '气压hPa: %s' % rep.json()['pressure'] + '\n'
msg1 += '空气质量: %s' % rep.json()['air'] + '\n'
msg1 += '空气质量等级: %s' % rep.json()['air_level'] + '\n'
msg1 += '空气质量描述: %s' % rep.json()['air_tips'] + '\n'
self.ui.resultText.setText(msg1)
def clearResult(self):
print('* clearResult ')
self.ui.resultText.clear()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.initComboxProvince()
win.show()
sys.exit(app.exec_())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。