1.完整代碼:

import time import urllib.request #發送網絡請求,獲取數據 import gzip #壓縮和解壓縮模塊 import json #解析獲得的數據 from tkinter import * root1 = Tk() #用tkinter建立根窗口 root1.title('天氣查詢xgj@V1.0')#窗口標題 root1.geometry('1300x800+500+0') #注意x=是小寫的字母x,不是乘號 root1.configure(bg='black') #構建一個函數,bg=背景顏色設置 Label(root1,text = '請輸入要查詢天氣的城市:',font=10,bg='black',fg='purple').grid(row=0,column=0)#設置標簽並調整位置 enter = Entry(root1,font=10,fg='purple')#輸入框設置,字體大小 enter.grid(row = 0,column=1,padx = 20, pady = 20)#調整位置 enter.insert(0,'北京')#設置默認文本,初始為北京 def get_weather_data() :#爬蟲部分:獲取網站數據 city_name = enter.get()#獲取輸入框的內容 url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name) weather_data = urllib.request.urlopen(url1).read() #讀取網頁數據 weather_data = gzip.decompress(weather_data).decode('utf-8') #解碼並解壓網頁數據 weather_dict = json.loads(weather_data) #將json數據轉換為dict數據 #增加判斷 if weather_dict.get('desc') == 'invilad-citykey': #輸入錯誤提示彈出框 print(messagebox.askokcancel("提示","你輸入的城市名有誤,或者天氣中心未收錄你所在城市")) else: show_data(weather_dict,city_name) #如果正確,那就進入展示數據窗口和內容 #定義展示數據窗口和內容設置 def show_data(weather_dict,city_name):#顯示數據窗口的設置 forecast = weather_dict.get('data').get('forecast')#獲取數據塊 root2=Tk()#彈出天氣查詢結果的窗口,第2個窗口 root2.geometry('1500x500+500+0')#修改窗口大小 root2.configure(bg='black') root2.title(city_name + '的未來5天的天氣狀況')#副窗口標題 #設置日期列表 for i in range(5):#將每一天的數據放入列表中 ,列表中5個數據 LANGS = [(forecast[i].get('date'),'日期'), (forecast[i].get('fengxiang'),'風向'), (forecast[i].get('high'),'最高溫'), (forecast[i].get('low'),'最低溫'), (forecast[i].get('type'),'天氣')] group = LabelFrame(root2,text = '天氣狀況',font=6,bg='black',fg='purple',padx = 0,pady = 0)#框架 group.pack(padx=11,pady=0,side = LEFT)#放置框架 for lang, value in LANGS:#將數據放入框架中 c = Label(group,text = value + ': ' + lang,fg='green',font=8,bg='black') c.pack(anchor = W) #第2個窗口root2的標簽和按鈕設置 Label(root2,font=10,text = '今日:' + weather_dict.get('data').get('ganmao'), fg = 'red',bg='black').place(x=40,y=20,height=40)#溫馨提示 Label(root2,text = "↑這是今天的天氣",font=8,fg = "red",bg = "black").place(x=10,y=450,width=300,height=50)#作者網站 Button(root2,text = '確認並退出',font=10,fg = "purple",bg = "black",command = root2.quit).place(x=1200,y=450,width = 200,height=50)#退出按鈕 root2.mainloop() #主界面:第1個窗口的按鈕設置布置按鍵 Button(root1, text = "確認",width=10,font=10,bg='black',fg='purple',command = get_weather_data)\ .grid(row = 3, column=0,sticky = W, padx = 10, pady = 5) Button(root1, text = '退出',width=10,font=10,bg='black',fg='purple',command = root1.quit)\ .grid(row = 3, column = 1, sticky = E, padx = 10, pady = 5) # 配置,放在這里,那么主窗口的2個按鈕就先顯示出來 # 要打開的圖像 image1 = "open.gif" #注意tkinter中需要gif格式的圖片,默認目錄下 image2 = "welcome.gif" #也可以單獨設置目錄或者路徑 #python3的特性,一行依次賦值 x0, y0=50.0,50.0 # 初始坐標 x,y=[x0],[y0] # 列表將包含所有的x和y坐標.到目前為止,他們只包含初始坐標 vx,vy=1.0,0.5 # 每次移動的速度或距離 # 邊界,這里要考慮到圖片的大小,要預留一半的長和寬 x_min,y_min,x_max,y_max=46.0,46.0,754.0,554.0 range_min,range_max=1,2000 # 運行步數 # 創建500次的x和y坐標 for t in range(range_min, range_max): # 新坐標等於舊坐標加每次移動的距離 new_x = x[t - 1] + vx new_y = y[t - 1] + vy # 如果已經越過邊界,反轉方向 if new_x >= x_max or new_x <= x_min: vx = vx * -1.0 if new_y >= y_max or new_y <= y_min: vy = vy * -1.0 # 添加新的值到列表 x.append(new_x) y.append(new_y) canvas = Canvas(root1,width=800, height=600, bg='white') canvas.grid(row = 2,column=0)#調整位置 photo1 = PhotoImage(file=image1) photo2 = PhotoImage(file=image2) #add width1 = photo1.width() height1 = photo1.height() image_x = (width1) / 2.0 image_y = (height1) / 2.0 # 每次的移動 for t in range(range_min, range_max): canvas.create_image(x[t], y[t], image=photo1, tag="pic") #tag是這張圖片的標簽,這里需要 canvas.update() # 暫停多少秒,然后刪除圖像 time.sleep(0.001) #1秒,緩慢;如果0.025s等於每秒40幀;0.001s很快 canvas.delete("pic") #因為這里需要刪除它 #等上面動畫結束后,再出現這個圖片 canvas.create_image(400, 300, image=photo2) #這里不需要tag canvas.update() #主窗口循環走起 root1.mainloop()
2. 圖片可自定義:注意是gif格式。