一、實(shí)現(xiàn)效果
1. python代碼
import requests from lxml import etree import re import tkinter as tk from PIL import Image, ImageTk from xpinyin import Pinyin def get_image(file_nam, width, height): im = Image.open(file_nam).resize((width, height)) return ImageTk.PhotoImage(im) def spider(): headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24', "referer": "/tupian/20230522/index.html" } p = Pinyin() place = ''.join(p.get_pinyin(b1.get()).split('-')) # 獲取地區(qū)文本框的輸入 變?yōu)槠匆? # 處理用戶(hù)輸入的時(shí)間 # 規(guī)定三種格式都可以 2018/10/1 2018年10月1日 2018-10-1 date = b2.get() # 獲取時(shí)間文本框的輸入 if '/' in date: tm_list = date.split('/') elif '-' in date: tm_list = date.split('-') else: tm_list = re.findall(r'\d+', date) if int(tm_list[1]) < 10: # 1-9月 前面加 0 tm_list[1] = f'0{tm_list[1]}' # 分析網(wǎng)頁(yè)規(guī)律 構(gòu)造url # 直接訪問(wèn)有該月所有天氣信息的頁(yè)面 提高查詢(xún)效率 url = f"https://lishi.tianqi.com/{place}/{''.join(tm_list[:2])}.html" resp = requests.get(url, headers=headers) html = etree.HTML(resp.text) # xpath定位提取該日天氣信息 info = html.xpath(f'//ul[@class="thrui"]/li[{int(tm_list[2])}]/div/text()') # 輸出信息格式化一下 info1 = ['日期:', '最高氣溫:', '最低氣溫:', '天氣:', '風(fēng)向:'] datas = [i + j for i, j in zip(info1, info)] info = '\n'.join(datas) t.insert('insert', ' 查詢(xún)結(jié)果如下 \n\n') t.insert('insert', info) print(info) win = tk.Tk() win.title('全國(guó)各地歷史天氣查詢(xún)系統(tǒng)') win.geometry('500x500') # 畫(huà)布 設(shè)置背景圖片 canvas = tk.Canvas(win, height=500, width=500) im_root = get_image('test.jpg', width=500, height=500) canvas.create_image(250, 250, image=im_root) canvas.pack() # 單行文本 L1 = tk.Label(win, bg='yellow', text="地區(qū):", font=("SimHei", 12)) L2 = tk.Label(win, bg='yellow', text="時(shí)間:", font=("SimHei", 12)) L1.place(x=85, y=100) L2.place(x=85, y=150) # 單行文本框 可采集鍵盤(pán)輸入 b1 = tk.Entry(win, font=("SimHei", 12), show=None, width=35) b2 = tk.Entry(win, font=("SimHei", 12), show=None, width=35) b1.place(x=140, y=100) b2.place(x=140, y=150) # 設(shè)置查詢(xún)按鈕 a = tk.Button(win, bg='red', text="查詢(xún)", width=25, height=2, command=spider) a.place(x=160, y=200) # 設(shè)置多行文本框 寬 高 文本框中字體 選中文字時(shí)文字的顏色 t = tk.Text(win, width=30, height=8, font=("SimHei", 18), selectforeground='red') # 顯示多行文本 t.place(x=70, y=280) # 進(jìn)入消息循環(huán) win.mainloop()
分享標(biāo)題:Pythontkinter界面實(shí)現(xiàn)歷史天氣查詢(xún)的示例代碼-創(chuàng)新互聯(lián)
本文來(lái)源:http://jinyejixie.com/article44/coshhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、網(wǎng)站改版、企業(yè)建站、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站策劃、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容