python 시각 화 파충류 인터페이스의 날씨 조회

실행 효 과 는 다음 과 같 습 니 다:


from tkinter import *
import urllib.request
import gzip
import json
from tkinter import messagebox
root = Tk()
def main():
  #     
  root.title('Python     :973783996') #     
  Label(root, text='     ').grid(row=0, column=0) #          
  enter = Entry(root) #    
  enter.grid(row=0, column=1, padx=20, pady=20) #     
  enter.delete(0, END) #      
  enter.insert(0, 'Python     :973783996') #       
  # enter_text = enter.get()#        
   running = 1
   def get_weather_data(): #       
    city_name = enter.get() #         
    url1 = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
    url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
    #   1        ,  2        
    # print(url1)
    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("xing", "         ,              "))
    else:
      # print(messagebox.askokcancel('xing','bingguo'))
      show_data(weather_dict, city_name)  def show_data(weather_dict, city_name): #     
    forecast = weather_dict.get('data').get('forecast') #      
    root1 = Tk() #    
    root1.geometry('650x280') #       
    root1.title(city_name + '    ') #      
     #       
    for i in range(5): #             
      LANGS = [(forecast[i].get('date'), '  '),
           (forecast[i].get('fengxiang'), '  '),
           (str(forecast[i].get('fengji')), '  '),
           (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: #         
        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) #     
    Button(root1, text='     ', width=10, command=root1.quit).place(x=500, y=230, width=80, height=40) #     
    root1.mainloop() 
  #     
  Button(root, text="  ", 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()
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기