Python腳本制作天氣查詢(xún)實(shí)例代碼
獲取天氣的主要代碼如下:
# cityCode 替換為具體某一個(gè)城市的對(duì)應(yīng)編號(hào)
# 1、發(fā)送請(qǐng)求,獲取數(shù)據(jù)
url = f'http://wthrcdn.etouch.cn/weather_mini?citykey={cityCode}'
res = requests.get(url)
res.encoding = 'utf-8'
res_json = res.json()
# 2、數(shù)據(jù)格式化
data = res_json['data']
city = f"城市:{data['city']}\n"
# 字符串格式化的一種方式 f"{}" 通過(guò)字典傳遞值
today = data['forecast'][0]
date = f"日期:{today['date']}\n" # \n 換行
now = f"實(shí)時(shí)溫度:{data['wendu']}度\n"
temperature = f"溫度:{today['high']} {today['low']}\n"
fengxiang = f"風(fēng)向:{today['fengxiang']}\n"
type = f"天氣:{today['type']}\n"
tips = f"貼士:{data['ganmao']}\n"
result = city + date + now + temperature + fengxiang + type + tips
print(result)
1、使用Qt Designer繪制窗口,保存為ui文件
2、把ui文件轉(zhuǎn)為py文件
(1)在生成的ui文件目錄下,打開(kāi)cmd
(2)輸入以下命令(注意替換名稱(chēng))
pyuic5 -o destination.py source.ui
3、信號(hào)與槽函數(shù)的連接
# 1、清空按鈕與對(duì)應(yīng)函數(shù)連接 clearBtn.clicked.connect(widget.clearResult) # 2、查詢(xún)按鈕與對(duì)應(yīng)函數(shù)連接 queryBtn.clicked.connect(widget.queryWeather)
4、調(diào)用主窗口類(lèi)
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)
# 通過(guò)文本框傳入想要搜索的城市名稱(chēng):天津
cityName = self.ui.weatherComboBox.currentText()
# 獲取天氣部分省略
# 在文本框顯示查詢(xún)結(jié)果
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_())
代碼擴(kuò)展:
from tkinter import *
import urllib.request
import gzip
import json
from tkinter import messagebox
root = Tk()
def main():
# 輸入窗口
root.title('Python學(xué)習(xí)交流群:973783996') # 窗口標(biāo)題
Label(root, text='請(qǐng)輸入城市').grid(row=0, column=0) # 設(shè)置標(biāo)簽并調(diào)整位置
enter = Entry(root) # 輸入框
enter.grid(row=0, column=1, padx=20, pady=20) # 調(diào)整位置
enter.delete(0, END) # 清空輸入框
enter.insert(0, 'Python學(xué)習(xí)交流群:973783996') # 設(shè)置默認(rèn)文本
# enter_text = enter.get()#獲取輸入框的內(nèi)容
running = 1
def get_weather_data(): # 獲取網(wǎng)站數(shù)據(jù)
city_name = enter.get() # 獲取輸入框的內(nèi)容
url1 = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
# 網(wǎng)址1只需要輸入城市名,網(wǎng)址2需要輸入城市代碼
# print(url1)
weather_data = urllib.request.urlopen(url1).read()
# 讀取網(wǎng)頁(yè)數(shù)據(jù)
weather_data = gzip.decompress(weather_data).decode('utf-8')
# 解壓網(wǎng)頁(yè)數(shù)據(jù)
weather_dict = json.loads(weather_data)
# 將json數(shù)據(jù)轉(zhuǎn)換為dict數(shù)據(jù)
if weather_dict.get('desc') == 'invilad-citykey':
print(messagebox.askokcancel("xing", "你輸入的城市名有誤,或者天氣中心未收錄你所在城市"))
else:
# print(messagebox.askokcancel('xing','bingguo'))
show_data(weather_dict, city_name) def show_data(weather_dict, city_name): # 顯示數(shù)據(jù)
forecast = weather_dict.get('data').get('forecast') # 獲取數(shù)據(jù)塊
root1 = Tk() # 副窗口
root1.geometry('650x280') # 修改窗口大小
root1.title(city_name + '天氣狀況') # 副窗口標(biāo)題
# 設(shè)置日期列表
for i in range(5): # 將每一天的數(shù)據(jù)放入列表中
LANGS = [(forecast[i].get('date'), '日期'),
(forecast[i].get('fengxiang'), '風(fēng)向'),
(str(forecast[i].get('fengji')), '風(fēng)級(jí)'),
(forecast[i].get('high'), '最高溫'),
(forecast[i].get('low'), '最低溫'),
(forecast[i].get('type'), '天氣')]
group = LabelFrame(root1, text='天氣狀況', padx=0, pady=0) # 框架
group.pack(padx=11, pady=0, side=LEFT) # 放置框架
for lang, value in LANGS: # 將數(shù)據(jù)放入框架中
c = Label(group, text=value + ': ' + lang)
c.pack(anchor=W)
Label(root1, text='今日' + weather_dict.get('data').get('ganmao'),
fg='green').place(x=40, y=20, height=40) # 溫馨提示
Label(root1, text="StarMan: 49star.com", fg="green", bg="yellow").place(x=10, y=255, width=125, height=20) # 作者網(wǎng)站
Button(root1, text='確認(rèn)并退出', width=10, command=root1.quit).place(x=500, y=230, width=80, height=40) # 退出按鈕
root1.mainloop()
# 布置按鍵
Button(root, text="確認(rèn)", width=10, command=get_weather_data) \
.grid(row=3, column=0, sticky=W, padx=10, pady=5)
Button(root, text='退出', width=10, command=root.quit) \
.grid(row=3, column=1, sticky=E, padx=10, pady=5)
if running == 1:
root.mainloop()
if __name__ == '__main__':
main()
到此這篇關(guān)于Python腳本制作天氣查詢(xún)實(shí)例代碼的文章就介紹到這了,更多相關(guān)Python腳本如何制作天氣查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django 限制用戶(hù)訪(fǎng)問(wèn)頻率的中間件的實(shí)現(xiàn)
這篇文章主要介紹了Django 限制用戶(hù)訪(fǎng)問(wèn)頻率的中間件的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Python學(xué)習(xí)之字典的創(chuàng)建和使用
這篇文章主要為大家介紹了Python中的字典的創(chuàng)建與使用,包括使用字典(添加、刪除、修改等操作),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06
Python 讀取有公式cell的結(jié)果內(nèi)容實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于Python 如何讀取有公式cell的結(jié)果內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-02-02
Python使用apscheduler模塊設(shè)置定時(shí)任務(wù)的實(shí)現(xiàn)
本文主要介紹了Python使用apscheduler模塊設(shè)置定時(shí)任務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
python機(jī)器學(xué)習(xí)庫(kù)xgboost的使用
這篇文章主要介紹了python機(jī)器學(xué)習(xí)庫(kù)xgboost的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
python爬蟲(chóng)把url鏈接編碼成gbk2312格式過(guò)程解析
這篇文章主要介紹了python爬蟲(chóng)把url鏈接編碼成gbk2312格式過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Python?return函數(shù)返回值類(lèi)型和幫助函數(shù)使用教程
這篇文章主要為大家介紹了Python?return函數(shù)返回值類(lèi)型和幫助函數(shù)使用教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

