-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather_display.py
195 lines (177 loc) · 10.5 KB
/
Weather_display.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import datetime
import json
import os
import requests
import sys
from sys import argv
import time
import tkinter as tk
from tkinter import ttk, PhotoImage, Frame
import webbrowser
# This loads the config values from other python files
import DarkSkyConfig
import Weather_Display_Config as ConfigSet
fileExt = "png"
try:
disposable = PhotoImage(file="images/cloudy.png")
except:
fileExt = "gif"
class Window(ttk.Frame):
def __init__(self, master=None):
ttk.Frame.__init__(self, master)
self.master = master
self.loopcount = 24
os.chdir(os.path.dirname(os.path.realpath(__file__)))
# Variables to use
# Current variables
self.timeText = tk.StringVar()
self.timeText.set("00:00AM")
self.currentPhotoImage = PhotoImage(file="images/cloudy." + fileExt)
self.currentPhotoImageText = tk.StringVar()
self.currentPhotoImageText.set("Null")
self.currentTempText = tk.StringVar()
self.currentTempText.set("0.0")
self.apparentTempText = tk.StringVar()
self.apparentTempText.set("(0.0)")
self.currentSummaryText = tk.StringVar()
self.currentSummaryText.set("Unknown")
# Sun rise/set vars
self.risePhotoImage = PhotoImage(file="images_small/sun-rise." + fileExt)
self.setPhotoImage = PhotoImage(file="images_small/sun-set." + fileExt)
# Day vars condenced
self.daysVars = {}
for i in range(0, 5):
self.daysVars["day" + str(i)] = {}
self.daysVars["day" + str(i)]["NameText"] = tk.StringVar()
self.daysVars["day" + str(i)]["NameText"].set("Nul")
self.daysVars["day" + str(i)]["PhotoImage"] = PhotoImage(file="images_small/cloudy." + fileExt)
self.daysVars["day" + str(i)]["MinText"] = tk.StringVar()
self.daysVars["day" + str(i)]["MinText"].set("Min:\n00")
self.daysVars["day" + str(i)]["MaxText"] = tk.StringVar()
self.daysVars["day" + str(i)]["MaxText"].set("Max:\n00")
self.daysVars["day" + str(i)]["RiseText"] = tk.StringVar()
self.daysVars["day" + str(i)]["RiseText"].set("00:00")
self.daysVars["day" + str(i)]["SetText"] = tk.StringVar()
self.daysVars["day" + str(i)]["SetText"].set("00:00")
self.daysVars["day" + str(i)]["SummaryText"] = tk.StringVar()
self.daysVars["day" + str(i)]["SummaryText"].set("Unknown")
# Widgets
# Frames
self.nowFrame = Frame(root)
self.daysFrame = Frame(root)
self.daysFrameCollection = {}
self.daysWidgetCollection = {}
self.day0Frame = Frame(self.daysFrame)
self.day1Frame = Frame(self.daysFrame)
self.day2Frame = Frame(self.daysFrame)
self.day3Frame = Frame(self.daysFrame)
self.day4Frame = Frame(self.daysFrame)
# Current widgets
# textvariable automatically updates with the StringVar() it's assigned to
self.timeLabel = ttk.Label(self.nowFrame, textvariable=self.timeText, font=("arial", 40, "bold"))
self.timeLabel.grid()
self.currentPhoto = ttk.Label(self.nowFrame, image=self.currentPhotoImage)
self.currentPhoto.grid(column=0, row=1)
self.currentPhotoText = ttk.Label(self.nowFrame, textvariable=self.currentPhotoImageText, font=("arial", 21, "bold"))
self.currentPhotoText.grid(column=0, row=2)
self.currentTemp = ttk.Label(self.nowFrame, textvariable=self.currentTempText, font=("arial", 58, "bold"))
self.currentTemp.grid(column=0, row=3)
self.apparentTemp = ttk.Label(self.nowFrame, textvariable=self.apparentTempText, font=("arial", 12))
self.apparentTemp.grid(column=0, row=4)
self.currentSummary = ttk.Label(self.nowFrame, textvariable=self.currentSummaryText, font=("arial", 10), wraplength=300)
self.currentSummary.grid(column=0, row=5)
self.poweredBy = ttk.Label(self.nowFrame, text="Powered By DarkSky", font=("arial", 8), foreground="white", cursor="hand2")
self.poweredBy.grid(column=0, row=6)
self.poweredBy.bind("<Button-1>", openApiLink)
# Day widgets (condenced)
for i in range(0, 5):
self.daysFrameCollection["day" + str(i)] = Frame(self.daysFrame)
self.daysWidgetCollection["day" + str(i)] = {}
self.daysWidgetCollection["day" + str(i)]["Name"] = ttk.Label(self.daysFrameCollection["day" + str(i)], textvariable=self.daysVars["day" + str(i)]["NameText"], font=("arial", 12, "bold"))
self.daysWidgetCollection["day" + str(i)]["Name"].grid()
self.daysWidgetCollection["day" + str(i)]["Photo"] = ttk.Label(self.daysFrameCollection["day" + str(i)], image=self.daysVars["day" + str(i)]["PhotoImage"])
self.daysWidgetCollection["day" + str(i)]["Photo"].grid(column=1, row=1, rowspan=2)
self.daysWidgetCollection["day" + str(i)]["Max"] = ttk.Label(self.daysFrameCollection["day" + str(i)], textvariable=self.daysVars["day" + str(i)]["MaxText"], font=("arial", 18, "bold"))
self.daysWidgetCollection["day" + str(i)]["Max"].grid(column=2, row=1, padx=10, rowspan=2)
self.daysWidgetCollection["day" + str(i)]["Min"] = ttk.Label(self.daysFrameCollection["day" + str(i)], textvariable=self.daysVars["day" + str(i)]["MinText"], font=("arial", 18, "bold"))
self.daysWidgetCollection["day" + str(i)]["Min"].grid(column=3, row=1, padx=10, rowspan=2)
self.daysWidgetCollection["day" + str(i)]["RisePhoto"] = ttk.Label(self.daysFrameCollection["day" + str(i)], image=self.risePhotoImage)
self.daysWidgetCollection["day" + str(i)]["RisePhoto"].grid(column=4, row=1, padx=5)
self.daysWidgetCollection["day" + str(i)]["Rise"] = ttk.Label(self.daysFrameCollection["day" + str(i)], textvariable=self.daysVars["day" + str(i)]["RiseText"], font=("arial", 18, "bold"))
self.daysWidgetCollection["day" + str(i)]["Rise"].grid(column=4, row=2, padx=5)
self.daysWidgetCollection["day" + str(i)]["SetPhoto"] = ttk.Label(self.daysFrameCollection["day" + str(i)], image=self.setPhotoImage)
self.daysWidgetCollection["day" + str(i)]["SetPhoto"].grid(column=5, row=1, padx=5)
self.daysWidgetCollection["day" + str(i)]["Set"] = ttk.Label(self.daysFrameCollection["day" + str(i)], textvariable=self.daysVars["day" + str(i)]["SetText"], font=("arial", 18, "bold"))
self.daysWidgetCollection["day" + str(i)]["Set"].grid(column=5, row=2, padx=5)
self.daysWidgetCollection["day" + str(i)]["Summary"] = ttk.Label(self.daysFrameCollection["day" + str(i)], textvariable=self.daysVars["day" + str(i)]["SummaryText"], font=("arial", 8), wraplength=100)
self.daysWidgetCollection["day" + str(i)]["Summary"].grid(column=6, row=0, rowspan=3)
# Setting the frames
self.nowFrame.pack(side="left")
self.daysFrame.pack(side="right", expand="true")
for i in range(0, 5):
self.daysFrameCollection["day" + str(i)].grid(row=i)
#Setting the background colour for all elements
self.frameList = [self.nowFrame, self.daysFrame, self.daysFrameCollection["day0"], self.daysFrameCollection["day1"], self.daysFrameCollection["day2"], self.daysFrameCollection["day3"], self.daysFrameCollection["day4"]]
for currentframe in self.frameList:
currentframe.configure(background=ConfigSet.BGColor)
for currentWidget in currentframe.winfo_children():
currentWidget.configure(background=ConfigSet.BGColor)
def DisUpdate():
if app.loopcount == 24:
VarSet()
app.loopcount += 1
# This displays the local time in a 12 hour format
app.timeText.set(time.strftime("%I:%M%p"))
root.update_idletasks()
#This loops the current function after 5 seconds
root.after(5000, DisUpdate)
return()
def VarSet():
app.loopcount = -1
# This attempts to get the data, if it fails, it tries again on the next loop around
try:
#content = json.loads(open("darksky_file.json").read())
content = json.loads(requests.get("https://api.darksky.net/forecast/" + DarkSkyConfig.ApiKey + "/" + DarkSkyConfig.Lat + "," + DarkSkyConfig.Long + "?units=" + DarkSkyConfig.Units + "&language=" + DarkSkyConfig.Language).text)
tempMeasurement = ""
roundto = 0
if content["flags"]["units"] == "us":
tempMeasurement = "°F"
roundto = 0
else:
tempMeasurement = "°C"
roundto = 1
except:
return
# Current values
# Update the image by updating the variable then the label
app.currentPhotoImage = PhotoImage(file="images/"+ content["currently"]["icon"] +"." + fileExt)
app.currentPhoto.configure(image=app.currentPhotoImage)
app.currentTempText.set(str(round(content["currently"]["temperature"],roundto)) + tempMeasurement)
app.apparentTempText.set(str(round(content["currently"]["apparentTemperature"],roundto)) + tempMeasurement)
app.currentPhotoImageText.set(content["currently"]["icon"])
app.currentSummaryText.set(content["minutely"]["summary"])
# Day values condenced
for i in range(0, 5):
app.daysVars["day" + str(i)]["NameText"].set(datetime.datetime.fromtimestamp(content["daily"]["data"][i]["time"]).strftime("%a"))
app.daysVars["day" + str(i)]["PhotoImage"] = PhotoImage(file="images_small/" + content["daily"]["data"][i]["icon"] + "." + fileExt)
app.daysWidgetCollection["day" + str(i)]["Photo"].configure(image=app.daysVars["day" + str(i)]["PhotoImage"])
app.daysVars["day" + str(i)]["MinText"].set("Min:\n" + str(round(content["daily"]["data"][i]["temperatureLow"],roundto)))
app.daysVars["day" + str(i)]["MaxText"].set("Max:\n" + str(round(content["daily"]["data"][i]["temperatureHigh"],roundto)))
app.daysVars["day" + str(i)]["RiseText"].set(datetime.datetime.fromtimestamp(content["daily"]["data"][i]["sunriseTime"]).strftime("%I:%M"))
app.daysVars["day" + str(i)]["SetText"].set(datetime.datetime.fromtimestamp(content["daily"]["data"][i]["sunsetTime"]).strftime("%I:%M"))
app.daysVars["day" + str(i)]["SummaryText"].set(content["daily"]["data"][i]["summary"])
def openApiLink(event):
webbrowser.open("https://darksky.net/poweredby/")
# Creating the window
root = tk.Tk()
app = Window(root)
app.master.title("Weather Display")
if "-f" in argv:
root.attributes("-fullscreen", True)
else:
app.master.geometry("800x480")
root.minsize(800,480)
# Loads the DisUpdate function after loading the window
root.after(5000, DisUpdate)
root.configure(background=ConfigSet.BGColor)
root.mainloop()