If you want to see how the things you make this time work, please see here (youtube video).
First, access OpenWeatherMap here. Then, when you log in, a screen like this will be displayed, so go to the API above it. Then, to get the current weather data, press Subscribe in Currbnt Weather Data. Then press Free's Get API Key. Then you will be taken to the first screen, so press API Keys. Then you can see the obtained API Key.
get_weather_data.py
import requests
import json
import schedule
api_key = '8c5752202e9c558f8d76310d5d96ee03'
city_name = 'Tokyo'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}&lang=ja'
response = requests.get(url)
def show_data():
response = requests.get(url)
data = json.loads(response.text)
print(data)
#data['weather'][0]['description']
If you execute show_data (), you will get the following Json data. You can get the result in Japanese by adding lang = ja at the end of the url.
{'coord': {'lon': 139.69, 'lat': 35.69}, 'weather': [{'id': 500, 'main': 'Rain', 'description': 'light rain', 'icon': '10d'}], 'base': 'stations', 'main': {'temp': 293.2, 'feels_like': 294.56, 'temp_min': 292.15, 'temp_max': 294.26, 'pressure': 1008, 'humidity': 93}, 'visibility': 7000, 'wind': {'speed': 2.6, 'deg': 80}, 'rain': {'1h': 0.64}, 'clouds': {'all': 75}, 'dt': 1589781754, 'sys': {'type': 1, 'id': 8077, 'country': 'JP', 'sunrise': 1589744047, 'sunset': 1589794888}, 'timezone': 32400, 'id': 1850144, 'name': 'Tokyo', 'cod': 200}
weather.py
import requests
import json
import datetime
import tkinter as tk
class Weather:
def __init__(self,parent):
self.api_key = '2d3e148162f779382b38d219e60e028e'
self.city_name = 'Tokyo'
self.url = f"http://api.openweathermap.org/data/2.5/weather?q={self.city_name}&appid={self.api_key}&lang=ja"
self.label = tk.Label(parent,text="weather data")
self.label2 = tk.Label(parent,text="updated time")
self.label.pack()
self.label2.pack()
self.label.after(60000,self.change_info)
self.label2.after(60000,self.change_info)
def show_data(self):
response = requests.get(self.url)
data = json.loads(response.text)
print(data['weather'][0]['description'],datetime.datetime.now())
return data
def change_info(self):
time = datetime.datetime.now()
data = self.show_data()
var = data['weather'][0]['description']
self.label.configure(text=var)
self.label2.configure(text=time)
self.label.after(60000,self.change_info)
self.label2.after(60000,self.change_info)
#if __name__ == '__main__':
# root = tk.Tk()
# weather = Weather(root)
# root.mainloop()
run = True
while run:
try:
root = tk.Tk()
weather = Weather(root)
root.mainloop()
except KeyboardInterrupt:
run = False
Again, I haven't done anything particularly difficult. Every minute, change_info () is called to get the data and update the GUI label. However, if you do it if you do not comment out when you finish it, it will end at the next update, so improvement is still needed. If you know how to get a good finish, please let me know. Please.
This method is also explained in Youtube, so please have a look if you like it. If you have any questions, please use the comment section of the video or the comment section of this article. Also, if you like it, please subscribe to the channel.
Recommended Posts