-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
159 lines (116 loc) · 5.94 KB
/
main.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
import customtkinter as ctk
from settings import *
from main_widgets import *
#* URL request
import urllib.request
import json
#* WEATHER
from weather_data import get_weather
from PIL import Image
from os import walk
try:
from ctypes import windll, byref, sizeof, c_int
except:
pass
class App(ctk.CTk):
def __init__(self, current_data, forecast_data, city, country):
self.current_data = current_data
# print(self.current_data)
self.forecast_data = forecast_data
self.location = {'city': city, 'country': country}
self.color = WEATHER_DATA[current_data['weather']]
#* Today Imports
# self.forecast_images = [Image.open(f"images/{info['weather']}.png") for info in self.forecast_data.values()] #! gets values of the dict
self.today_animation = self.import_image_folder(self.color['path'])
super().__init__(fg_color = self.color['main'])
self.change_title_bar(self.color['title'])
self.geometry('550x250')
self.minsize(550,250)
self.iconbitmap('empty.ico')
self.title('')
#* START WIDGET
self.widget = SmallWidget(self, self.current_data, self.location, self.color, self.today_animation)
#* STATES
self.height_break = 600
self.width_break = 1000
self.full_height_bool = ctk.BooleanVar(value = False) #! checks if window size exceeds the height break of 600px
self.full_width_bool = ctk.BooleanVar(value = False) #! Check width break
self.bind('<Configure>', self.check_size)
self.full_width_bool.trace('w', self.change_size)
self.full_height_bool.trace('w', self.change_size)
#* RUN
self.mainloop()
def import_image_folder(self, path):
for _, __, image_data in walk(path): #! goes throught all files in the folder
sorted_data = sorted(image_data, key = lambda item: int(item.split('.')[0])) #! gets only the stuff before .png and sorts
image_paths = [path + '/' + item for item in sorted_data]
images = [Image.open(path) for path in image_paths]
return images
def change_title_bar(self, color):
try:
HWND = windll.user32.GetParent(self.winfo_id())
windll.dwmapi.DwmSetWindowAttribute(HWND, 35, byref(c_int(color)), sizeof(c_int))
except:
pass
def check_size(self, event):
if event.widget == self: #! prevents calling configure on every widget, this ensures only the main window is applied
#* WIDTH
if self.full_width_bool.get(): #! if window width > 1000 px, check if the width becomes smaller or equal to 1000
if event.width < self.width_break:
self.full_width_bool.set(False)
else: #! window width < 1000 px, check if the width becomes bigger or equal to 1000
if event.width > self.width_break:
self.full_width_bool.set(True)
#* HEIGHT
if self.full_height_bool.get(): #! if window height > 600 px
if event.height < self.height_break:
self.full_height_bool.set(False)
else: #! window height < 600 px
if event.height > self.height_break:
self.full_height_bool.set(True)
def change_size(self, *args):
self.widget.pack_forget()
#* MAX WIDGET
if self.full_height_bool.get() and self.full_width_bool.get():
self.widget = MaxWidget(self,
current_data = self.current_data,
location = self.location,
forecast_data = self.forecast_data,
color = self.color,
# forecast_images = self.forecast_images,
animation = self.today_animation)
#* TALL WIDGET
if self.full_height_bool.get() and not self.full_width_bool.get():
self.widget = TallWidget(self,
current_data = self.current_data,
location = self.location,
forecast_data = self.forecast_data,
color = self.color,
# forecast_images = self.forecast_images,
animation = self.today_animation)
#* WIDE WIDGET
if not self.full_height_bool.get() and self.full_width_bool.get():
self.widget = WideWidget(self,
current_data = self.current_data,
location = self.location,
forecast_data = self.forecast_data,
color = self.color,
# forecast_images = self.forecast_images,
animation = self.today_animation)
#* SMALL WIDGET
if not self.full_height_bool.get() and not self.full_width_bool.get():
self.widget = SmallWidget(self, self.current_data, self.location, self.color,
animation = self.today_animation)
if __name__ == '__main__':
#*LOCATION
with urllib.request.urlopen("https://ipapi.co/json/") as url:
data = json.loads(url.read().decode())
city = data['city']
country = data['country_name']
latitude = data['latitude']
longitude = data['longitude']
#* WEATHER INFO
#! Will return dictionary of temp, feellike, and weather
current_data = get_weather(latitude, longitude, 'imperial', 'today')
forecast_data = get_weather(latitude, longitude, 'imperial', 'forecast')
App(current_data = current_data, forecast_data = forecast_data, city = city, country = country)