PyQt5實踐——《做一個天氣預報的查詢》
第一步: 設計界面,我們這里直接用Qtdesign設計。
為了大家比較容易設置,我們這里在把對象查看器中的內容貼出來
簡單的幾個組件:一個窗口+兩個按鈕+一個下拉是的盒子+一個標簽+一個返回文本顯示的內容
第二步:將ui文件轉換為py文件
將demo.ui
轉換成demo.py
文件的
方法一:手動點擊.ui
—Qt
—PyUIC
方法二:控制台命令
python -m PyQt5.uic.pyuic demo.ui -o demo.py
pyuic -o Weather3.0.py Weather3.0.ui
下面是轉換后的py文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'WeatherWin.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# 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(554, 396)
self.clearBtn = QtWidgets.QPushButton(Form)
self.clearBtn.setGeometry(QtCore.QRect(320, 320, 75, 23))
self.clearBtn.setObjectName("clearBtn")
self.groupBox = QtWidgets.QGroupBox(Form)
self.groupBox.setGeometry(QtCore.QRect(60, 30, 421, 251))
self.groupBox.setObjectName("groupBox")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(80, 39, 51, 21))
self.label.setObjectName("label")
self.weatherComboBox = QtWidgets.QComboBox(self.groupBox)
self.weatherComboBox.setGeometry(QtCore.QRect(140, 40, 221, 22))
self.weatherComboBox.setObjectName("weatherComboBox")
self.weatherComboBox.addItem("")
self.weatherComboBox.addItem("")
self.weatherComboBox.addItem("")
self.weatherComboBox.addItem("")
self.resultText = QtWidgets.QTextEdit(self.groupBox)
self.resultText.setGeometry(QtCore.QRect(70, 100, 291, 131))
self.resultText.setObjectName("resultText")
self.queryBtn = QtWidgets.QPushButton(Form)
self.queryBtn.setGeometry(QtCore.QRect(150, 320, 75, 23))
self.queryBtn.setObjectName("queryBtn")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.clearBtn.setText(_translate("Form", "清空"))
self.groupBox.setTitle(_translate("Form", "查詢城市天氣"))
self.label.setText(_translate("Form", "城市"))
self.weatherComboBox.setItemText(0, _translate("Form", "北京"))
self.weatherComboBox.setItemText(1, _translate("Form", "西安"))
self.weatherComboBox.setItemText(2, _translate("Form", "上海"))
self.weatherComboBox.setItemText(3, _translate("Form", "天津"))
self.queryBtn.setText(_translate("Form", "查詢"))
第三步: 數據傳遞
第二步只是將界面繪制了出來並轉換為py文件,接着我們會使用requests模塊對網站發起請求,獲取我們想要的數據,並顯示到我們的窗口中。
首先我們試着訪問一下該網址:http://www.weather.com.cn/data/sk/101010100.html 這個查的是北京的天氣,是一個json格式的數據,而且還亂碼了,沒關系,我們等會改成utf-8就好了。 每個城市之間只是那紅色的城市代碼不同。 我這里只找了北京,天津,西安,上海的城市代碼。所以只能查這些地區。。 如果想找你所在的城市,去網上找一下。這是我在網上隨便找的。我們可以構造查詢了連接,然后用requests進行請求。 返回json格式的數據,我們一步一步提取出來就可以了。 下面是調用的文件。
import sys
from PyQt5.QtWidgets import *
from Weather 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)
self.ui.clearBtn.clicked.connect(self.clearResult)
self.ui.queryBtn.clicked.connect(self.queryWeather)
def queryWeather(self):
print('* queryWeather')
cityName = self.ui.weatherComboBox.currentText()
cityCode = self.transCityName(cityName)
web = 'http://www.weather.com.cn/data/sk/' + cityCode + '.html'
rep = requests.get(web)
rep.encoding = 'utf-8'
# print(rep.json())
msg1 = '城市: %s' % rep.json()['weatherinfo']['city'] + '\n'
msg2 = '風向: %s' % rep.json()['weatherinfo']['WD'] + '\n'
msg3 = '溫度: %s' % rep.json()['weatherinfo']['temp'] + '\n'
msg4 = '風力: %s' % rep.json()['weatherinfo']['WS'] + '\n' + '度'
msg5 = '濕度: %s' % rep.json()['weatherinfo']['SD'] + '\n'
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'
elif cityName == '西安':
cityCode = '101110101'
return cityCode
def clearResult(self):
print('* clearResult')
self.ui.resultText.clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.setObjectName("MainWindow") # 為主窗口設置對象名 為了在下面設置背景顏色
win.setStyleSheet("#MainWindow{border-image:url(./images/python.jpg);}") # 這個圖片路徑是我自己的,你可以這一張你自己的圖片。或者把這兩行都給注釋掉
win.show()
sys.exit(app.exec_())
最后的界面:
看一下功能,有個下拉框 可以選擇城市,然后點擊查詢就可以查到了。