SHARE
TWEET

Python - weather API + Tkinter

furas Jun 8th, 2018 (edited) 337 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. import tkinter as tk
  3.  
  4. api_url = "http://weather.livedoor.com/forecast/webservice/json/v1"
  5. base = tk.Tk()
  6. base.title("Japan Weather") #title
  7. w = 10 #width of cell and col
  8.  
  9. ###### fixing ######
  10.  
  11. #list of the prefecture
  12.  
  13. data = [ # name, city_code, column, row
  14.     ("Hokkaido", "016010", 0, 0),
  15.     ("Aomori", "020010", 1, 0),
  16.     ("Iwate", "030010", 1, 1),
  17.     ("Miyagi", "040010", 1, 2),
  18.     ("Akita", "050010", 1, 3),
  19.     ("Yamagata", "060010", 1, 4),
  20.     ("Fukushima", "070010", 1, 5),
  21.     ("Ibaraki", "080010", 2, 0),
  22.     ("Tochigi", "090010", 2, 1),
  23.     ("Gunma", "100010", 2, 2),
  24.     ("Saitama", "110010", 2, 3),
  25.     ("Chiba", "120010", 2, 4),
  26.     ("Tokyo", "130010", 2, 5),
  27.     ("Kanagawa", "140010", 2, 6),
  28.     ("Niigata", "150010", 3, 0),
  29.     ("Toyama", "160010", 3, 1),
  30.     ("Ishikawa", "170010", 3, 2),
  31.     ("Fukui", "180010", 3, 3),
  32.     ("Yamanashi", "190010", 3, 4),
  33.     ("Nagano", "200010", 3, 5),
  34.     ("Gifu", "210010", 3, 6),
  35.     ("Shizuoka", "220010", 3, 7),
  36.     ("Aichi", "230010", 3, 8),
  37.     ("Mie", "240010", 4, 0),
  38.     ("Shiga", "250010", 4, 1),
  39.     ("Kyoto", "260010", 4, 2),
  40.     ("Osaka", "270010", 4, 3),
  41.     ("Hyogo", "280010", 4, 4),
  42.     ("Nara", "290010", 4, 5),
  43.     ("Wakayama", "300010", 4, 6),
  44.     ("Tototri", "310010", 5, 0),
  45.     ("Shimane", "320010", 5, 1),
  46.     ("Okayama", "330010", 5, 2),
  47.     ("Hiroshima", "340010", 5, 3),
  48.     ("Yamaguchi", "350010", 5, 4),
  49.     ("Tokushima", "360010", 6, 0),
  50.     ("Kagawa", "370010", 6, 1),
  51.     ("Ehime", "380010", 6, 2),
  52.     ("Kochi", "390010", 6, 3),
  53.     ("Fukuoka", "400010", 7, 0),
  54.     ("Saga", "410010", 7, 1),
  55.     ("Nagasaki", "420010", 7, 2),
  56.     ("Kumamoto", "430010", 7, 3),
  57.     ("Oita", "440010", 7, 4),
  58.     ("Miyazaki", "450010", 7, 5),
  59.     ("Kagoshima", "460010", 7, 6),
  60.     ("Okinawa", "471010", 8, 0),
  61. ]
  62.  
  63. # function of button / display each weather
  64.  
  65. def push(city_code):
  66.     weather_data = requests.get(api_url, params={'city': city}).json() #get city name
  67.     for i in weather_data["forecasts"]:
  68.         print(i["dateLabel"] + "の天気は、" + i["telop"])
  69.     print("\n")    
  70.  
  71. # commnd exit application
  72.  
  73. def exit():
  74.     base.destroy()    
  75.  
  76. # display menu_bar
  77. menu_bar = tk.Menu(base)
  78. file_menu = tk.Menu(menu_bar)
  79. menu_bar.add_cascade(label="File", menu=file_menu)
  80. file_menu.add_command(label = "Exit", command=exit)
  81. base.config(menu=menu_bar)
  82.  
  83. # display button
  84.  
  85. for name, city, c, r in data:
  86.     b = tk.Button(base, text=name, command=(lambda x= city:push(x)))
  87.     b.grid(column=c, row=r, sticky='we')
  88.  
  89. base.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top