-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
159 lines (120 loc) · 5.33 KB
/
app.py
File metadata and controls
159 lines (120 loc) · 5.33 KB
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 tkinter as tk
import requests
import webbrowser
API_KEY = "API_KEY_HERE"
# HEIGHT & WIDTH FOR FRAME
HEIGHT = 400
WIDTH = 600
# HEIGHT & WIDTH FOR MENU ITEMS
mHEIGHT = 150
mWIDTH = 420
root = tk.Tk()
# Get screen width and height
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# Calculate position x, y
x = (ws / 2) - (mWIDTH / 2)
y = (hs / 2) - (mHEIGHT / 2)
#h
class Window(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
def center_window(w, h):
# calculate position x, y
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
canvas = tk.Canvas(self.master, height=HEIGHT, width=WIDTH, bg='#61C9A8')
canvas.pack()
self.place(relx=0.5, rely=0.09, relwidth=0.2, relheight=0.1, anchor='n')
self.master.title("AIR QUALITY INDEX")
self.background_image = tk.PhotoImage(file='img/bg.png')
self.background_label = tk.Label(root, image=self.background_image)
self.background_label.place(relwidth=1, relheight=1)
frame = tk.Frame(self.master, bg='green')
frame.place(relx=0.5, rely=0.09, relwidth=0.2, relheight=0.1, anchor='n')
button = tk.Button(frame, text="Get AQI", bg='#BCF4DE', highlightthickness=0, font=40, command=self.pop_aqi)
button.place(relheight=1, relwidth=1)
my_menu = tk.Menu(self.master)
self.master.config(menu=my_menu)
# File menu
file_menu = tk.Menu(my_menu, tearoff=False, font=12)
my_menu.add_cascade(label="Air Quality Chart", menu=file_menu)
# File Menu items
file_menu.add_command(label="AQI Chart", command=self.aqi_chart)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.exitProgram)
# About Menu
about_menu = tk.Menu(my_menu, tearoff=False, font=12)
my_menu.add_cascade(label="Help", menu=about_menu)
# About Menu Items
about_menu.add_command(label="GET API KEY", command=self.api_key)
about_menu.add_separator()
about_menu.add_command(label="Support", command=self.support_me)
about_menu.add_separator()
about_menu.add_command(label="Feedback", command=self.feed_back)
about_menu.add_separator()
about_menu.add_command(label="About", command=self.about_app)
self.master.geometry("600x400")
self.master.resizable(False, False)
center_window(WIDTH, HEIGHT)
def exitProgram(self):
exit()
def aqi_chart(self):
top = tk.Toplevel()
top.title('Air Quality Index Chart')
lbl = tk.Label(top, text="\n 0-50 | Good \n"
"51-100 | Moderate \n"
"101-150 | Unhealthy for Sensitive Groups \n"
"151-200 | Unhealthy \n"
"201-300 | Very Unhealthy \n"
"300+ | Hazardous", justify='left', font=14).pack()
top.geometry('%dx%d+%d+%d' % (mWIDTH, mHEIGHT, x, y))
top.resizable(False, False)
# API KEY Hyperlink
def api_key(self):
webbrowser.open('https://www.iqair.com/', new=1)
def support_me(self):
top = tk.Toplevel()
top.title('About App')
lbl = tk.Label(top, text="\n \nYou can support me through GitHub.",
justify='left', font=12).pack()
lbl1 = tk.Label(top, text="\n https://github.com/abimsedhain",
justify='left', font=12).pack()
top.geometry('%dx%d+%d+%d' % (mWIDTH, mHEIGHT, x, y))
top.resizable(False, False)
def feed_back(self):
top = tk.Toplevel()
top.title('About App')
lbl = tk.Label(top, text="\n \nIf you have any feedback then tweet me. \n",
justify='left', font=12).pack()
lbl1 = tk.Label(top, text=" @ApexAbim \n \n",
justify='center', font=12).pack()
top.geometry('%dx%d+%d+%d' % (self.master.mWIDTH, self.master.mHEIGHT, self.master.x, self.master.y))
top.resizable(False, False)
def about_app(self):
top = tk.Toplevel()
top.title('About App')
lbl = tk.Label(top, text=" \n \n Air Quality Index App.", justify='center', font=12).pack()
lbl1 = tk.Label(top, text="\n Version 1.0.0 \n",
justify='left', font=12).pack()
top.geometry('%dx%d+%d+%d' % (mWIDTH, mHEIGHT, x, y))
top.resizable(False, False)
def pop_aqi(self):
ap_key = API_KEY
url = 'https://api.airvisual.com/v2/nearest_city?key=' + ap_key
response = requests.get(url)
aqi = response.json()
city = aqi['data']['city']
country = aqi['data']['country']
aqindex = aqi['data']['current']['pollution']['aqius']
temp = aqi['data']['current']['weather']['tp']
label = 'City: %s,%s \n \nAir Quality Index: %s \n \nTemp.(°C): %s' % (city, country, aqindex, temp)
top = tk.Toplevel()
top.title('Air Quality Index')
lbl = tk.Label(top, text='\n' + label, justify='left', font=14).pack()
top.geometry('%dx%d+%d+%d' % (mWIDTH, mHEIGHT, x, y))
top.resizable(False, False)
app = Window(root)
root.mainloop()