WeatherWin.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>450</width> <height>347</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QGroupBox" name="groupBox"> <property name="geometry"> <rect> <x>10</x> <y>10</y> <width>431</width> <height>251</height> </rect> </property> <property name="title"> <string>查询城市天气</string> </property> <widget class="QComboBox" name="weatherComboBox"> <property name="geometry"> <rect> <x>60</x> <y>30</y> <width>131</width> <height>21</height> </rect> </property> <item> <property name="text"> <string>北京</string> </property> </item> <item> <property name="text"> <string>天津</string> </property> </item> <item> <property name="text"> <string>上海</string> </property> </item> </widget> <widget class="QTextEdit" name="resultText"> <property name="geometry"> <rect> <x>10</x> <y>60</y> <width>411</width> <height>181</height> </rect> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>20</x> <y>30</y> <width>72</width> <height>21</height> </rect> </property> <property name="text"> <string>城市</string> </property> </widget> </widget> <widget class="QPushButton" name="queryBtn"> <property name="geometry"> <rect> <x>90</x> <y>300</y> <width>93</width> <height>28</height> </rect> </property> <property name="text"> <string>查询</string> </property> </widget> <widget class="QPushButton" name="clearBtn"> <property name="geometry"> <rect> <x>230</x> <y>300</y> <width>93</width> <height>28</height> </rect> </property> <property name="text"> <string>清空</string> </property> </widget> </widget> <resources/> <connections> <connection> <sender>clearBtn</sender> <signal>clicked()</signal> <receiver>Form</receiver> <slot>clearResult()</slot> <hints> <hint type="sourcelabel"> <x>270</x> <y>314</y> </hint> <hint type="destinationlabel"> <x>363</x> <y>288</y> </hint> </hints> </connection> <connection> <sender>queryBtn</sender> <signal>clicked()</signal> <receiver>Form</receiver> <slot>queryWeather()</slot> <hints> <hint type="sourcelabel"> <x>161</x> <y>313</y> </hint> <hint type="destinationlabel"> <x>64</x> <y>286</y> </hint> </hints> </connection> </connections> <slots> <slot>clearResult()</slot> <slot>queryWeather()</slot> </slots> </ui>
WeatherWin.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'WeatherWin.ui' # # Created by: PyQt5 UI code generator 5.9.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(450, 347) self.groupBox = QtWidgets.QGroupBox(Form) self.groupBox.setGeometry(QtCore.QRect(10, 10, 431, 251)) self.groupBox.setObjectName("groupBox") self.weatherComboBox = QtWidgets.QComboBox(self.groupBox) self.weatherComboBox.setGeometry(QtCore.QRect(60, 30, 131, 21)) self.weatherComboBox.setObjectName("weatherComboBox") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.resultText = QtWidgets.QTextEdit(self.groupBox) self.resultText.setGeometry(QtCore.QRect(10, 60, 411, 181)) self.resultText.setObjectName("resultText") self.label = QtWidgets.QLabel(self.groupBox) self.label.setGeometry(QtCore.QRect(20, 30, 72, 21)) self.label.setObjectName("label") self.queryBtn = QtWidgets.QPushButton(Form) self.queryBtn.setGeometry(QtCore.QRect(90, 300, 93, 28)) self.queryBtn.setObjectName("queryBtn") self.clearBtn = QtWidgets.QPushButton(Form) self.clearBtn.setGeometry(QtCore.QRect(230, 300, 93, 28)) self.clearBtn.setObjectName("clearBtn") self.retranslateUi(Form) self.clearBtn.clicked.connect(Form.clearResult) self.queryBtn.clicked.connect(Form.queryWeather) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.groupBox.setTitle(_translate("Form", "查询城市天气")) self.weatherComboBox.setItemText(0, _translate("Form", "北京")) self.weatherComboBox.setItemText(1, _translate("Form", "天津")) self.weatherComboBox.setItemText(2, _translate("Form", "上海")) self.label.setText(_translate("Form", "城市")) self.queryBtn.setText(_translate("Form", "查询")) self.clearBtn.setText(_translate("Form", "清空"))
getWeatherInfo.py
import requests rep = requests.get('http://www.weather.com.cn/data/sk/101010100.html') rep.encoding = 'utf-8' print('返回结果: %s' % rep.json() ) print('城市: %s' % rep.json()['weatherinfo']['city'] ) print('风向: %s' % rep.json()['weatherinfo']['WD'] ) print('温度: %s' % rep.json()['weatherinfo']['temp'] + " 度") print('风力: %s' % rep.json()['weatherinfo']['WS'] ) print('湿度: %s' % rep.json()['weatherinfo']['SD'] )
天气信息查询
''' 项目实战:天气信息查询 ''' import sys from PyQt5.QtWidgets import QApplication , QMainWindow from WeatherWin import Ui_Form import requests class MainWindow(QMainWindow ): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.ui = Ui_Form() self.ui.setupUi(self) def queryWeather(self): cityName = self.ui.weatherComboBox.currentText() cityCode = self.transCityName(cityName) rep = requests.get('http://www.weather.com.cn/data/sk/' + cityCode + '.html') rep.encoding = 'utf-8' print( rep.json() ) msg1 = '城市: %s' % rep.json()['weatherinfo']['city'] + ' ' msg2 = '风向: %s' % rep.json()['weatherinfo']['WD'] + ' ' msg3 = '温度: %s' % rep.json()['weatherinfo']['temp'] + ' 度' + ' ' msg4 = '风力: %s' % rep.json()['weatherinfo']['WS'] + ' ' msg5 = '湿度: %s' % rep.json()['weatherinfo']['SD'] + ' ' result = msg1 + msg2 + msg3 + msg4 + msg5 self.ui.resultText.setText(result) def transCityName(self ,cityName): cityCode = '' if cityName == '北京' : cityCode = '101010100' elif cityName == '天津' : cityCode = '101030100' elif cityName == '上海' : cityCode = '101020100' return cityCode def clearResult(self): print('* clearResult ') self.ui.resultText.clear() if __name__=="__main__": app = QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_())