Python腳本制作天氣查詢實(shí)例代碼_第1頁
Python腳本制作天氣查詢實(shí)例代碼_第2頁
Python腳本制作天氣查詢實(shí)例代碼_第3頁
Python腳本制作天氣查詢實(shí)例代碼_第4頁
Python腳本制作天氣查詢實(shí)例代碼_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第Python腳本制作天氣查詢實(shí)例代碼#1、發(fā)送請求,獲取數(shù)據(jù)

url=f'/weather_minicitykey={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"{}"通過字典傳遞值

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、使用QtDesigner繪制窗口,保存為ui文件

2、把ui文件轉(zhuǎn)為py文件

(1)在生成的ui文件目錄下,打開cmd

(2)輸入以下命令(注意替換名稱)

pyuic5-odestination.pysource.ui

3、信號與槽函數(shù)的連接

#1、清空按鈕與對應(yīng)函數(shù)連接

clearBtn.clicked.connect(widget.clearResult)

#2、查詢按鈕與對應(yīng)函數(shù)連接

queryBtn.clicked.connect(widget.queryWeather)

4、調(diào)用主窗口類

importsys

fromPyQt5.QtWidgetsimportQApplication,QMainWindow

fromWeatherWinimportUi_widget

importrequests

importjson

classMainWindow(QMainWindow):

def__init__(self,parent=None):

super(MainWindow,self).__init__(parent)

self.ui=Ui_widget()

self.ui.setupUi(self)

#通過文本框傳入想要搜索的城市名稱:天津

cityName=self.ui.weatherComboBox.currentText()

#獲取天氣部分省略

#在文本框顯示查詢結(jié)果

self.ui.resultText.setText(result)

defclearResult(self):

print('*clearResult')

self.ui.resultText.clear()

if__name__=="__main__":

app=QApplication(sys.argv)

win=MainWindow()

win.show()

sys.exit(app.exec_())

代碼擴(kuò)展:

fromtkinterimport*

importurllib.request

importgzip

importjson

fromtkinterimportmessagebox

root=Tk()

defmain():

#輸入窗口

root.title('Python學(xué)習(xí)交流群:973783996')#窗口標(biāo)題

Label(root,text='請輸入城市').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

defget_weather_data():#獲取網(wǎng)站數(shù)據(jù)

city_name=enter.get()#獲取輸入框的內(nèi)容

url1='/weather_minicity='+urllib.parse.quote(city_name)

url2='/weather_minicitykey=101010100'

#網(wǎng)址1只需要輸入城市名,網(wǎng)址2需要輸入城市代碼

#print(url1)

weather_data=urllib.request.urlopen(url1).read()

#讀取網(wǎng)頁數(shù)據(jù)

weather_data=gzip.decompress(weather_data).decode('utf-8')

#解壓網(wǎng)頁數(shù)據(jù)

weather_dict=json.loads(weather_data)

#將json數(shù)據(jù)轉(zhuǎn)換為dict數(shù)據(jù)

ifweather_dict.get('desc')=='invilad-citykey':

print(messagebox.askokcancel("xing","你輸入的城市名有誤,或者天氣中心未收錄你所在城市"))

else:

#print(messagebox.askokcancel('xing','bingguo'))

show_data(weather_dict,city_name)defshow_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è)置日期列表

foriinrange(5):#將每一天的數(shù)據(jù)放入列表中

LANGS=[(forecast[i].get('date'),'日期'),

(forecast[i].get('fengxiang'),'風(fēng)向'),

(str(forecast[i].get('fengji')),'風(fēng)級'),

(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)#放置框架

forlang,valueinLANGS:#將數(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:49",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=1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論