-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.py
56 lines (49 loc) · 2.24 KB
/
weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#Author: Dustin Grady
#Function: Access weather data from Open Weather Maps
#Status: Working/ Tested
import pyowm
import time
try:
#python3
from tkinter import *
except:
#python2
from Tkinter import *
API_KEY = 'Your_API_Key_Here'
time1 = time.localtime(time.time())
class WeatherClass():
def __init__(self):
owm = pyowm.OWM(API_KEY)
self.location = 'Santa Cruz, CA, USA'
observation = owm.weather_at_place(self.location)
weather = observation.get_weather()
'''Get Current Weather Conditions'''
self.currentWeather = str(weather.get_detailed_status().title())
'''Get Current Temperature in Fahrenheit'''
self.currentTemperature = weather.get_temperature(unit = 'fahrenheit')
self.currentTemperature = str(self.currentTemperature).split("'temp':", 1)[1]#Get temperature, removing everything before it
self.currentTemperature = str(self.currentTemperature[:3])#Remove everything except for the 2 digits we need
'''Determine Weather Icon'''
if "Clouds" in self.currentWeather:
self.weatherImage = PhotoImage(file="Cloudy.png")
if "Clear" in self.currentWeather:
if(time1.tm_hour > 6) and (time1.tm_hour <18):#Between 6am and 6pm will be day
self.weatherImage = PhotoImage(file="ClearSkyDay.png")
else:
self.weatherImage = PhotoImage(file="ClearSkyNight.png")
if "Sunny" in self.currentWeather:
self.weatherImage = PhotoImage(file="ClearSkyDay.png")
if "Rain" in self.currentWeather:
self.weatherImage = PhotoImage(file="Rain.png")
if "Drizzle" in self.currentWeather:
self.weatherImage = PhotoImage(file="Rain.png")
if "Thunder" in self.currentWeather:
self.weatherImage = PhotoImage(file="Thunder.png")
if "Snow" in self.currentWeather:
self.weatherImage = PhotoImage(file="Snow.png")
if "Haze" in self.currentWeather:
self.weatherImage = PhotoImage(file="Foggy.png")
if "Fog" in self.currentWeather:
self.weatherImage = PhotoImage(file="Foggy.png")
if "Mist" in self.currentWeather:
self.weatherImage = PhotoImage(file="Foggy.png")