-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
134 lines (97 loc) · 3.96 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
# importing the libraries
from tkinter import *
import requests
import json
import datetime
import os
from PIL import ImageTk, Image
os.system('cls')
root = Tk()
root.title("Weather App")
root.geometry("450x700")
root['background'] = "white"
# Logo
dir_path = os.path.dirname(os.path.realpath(__file__)) + '\\'
new = ImageTk.PhotoImage(Image.open(dir_path + 'logo.png'))
panel = Label(root, image=new)
panel.place(x=0, y=520)
# Date and Time
dt = datetime.datetime.now()
date = Label(root, text=dt.strftime('%A--'), bg='white', font=("bold", 15))
date.place(x=5, y=130)
month = Label(root, text=dt.strftime('%m %B'), bg='white', font=("bold", 15))
month.place(x=100, y=130)
hour = Label(root, text=dt.strftime('%I : %M %p'), bg='white', font=("bold", 15))
hour.place(x=10, y=160)
if int((dt.strftime('%I'))) >= 8 & int((dt.strftime('%I'))) <= 5:
img = ImageTk.PhotoImage(Image.open(dir_path + 'moon.png'))
panel = Label(root, image=img)
panel.place(x=210, y=200)
else:
img = ImageTk.PhotoImage(Image.open(dir_path + 'sun.png'))
panel = Label(root, image=img)
panel.place(x=210, y=200)
# City Search
city_name = StringVar()
city_entry = Entry(root, textvariable=city_name, width=45)
city_entry.grid(row=1, column=0, ipady=10, stick=W+E+N+S)
def city_name():
# API Call
api_key = '' # Enter your api-ID here
api_request = requests.get("https://api.openweathermap.org/data/2.5/weather?q="+ city_entry.get() + "&units=metric&appid="+api_key)
api = json.loads(api_request.content)
# Temperatures
y = api['main']
current_temprature = int(y['temp'])
humidity = y['humidity']
tempmin = y['temp_min']
tempmax = y['temp_max']
# Coordinates
x = api['coord']
longtitude = x['lon']
latitude = x['lat']
# Country
z = api['sys']
country = z['country']
citi = api['name']
# Adding the received info into the screen
lable_temp.configure(text=current_temprature)
lable_humidity.configure(text=humidity)
max_temp.configure(text=tempmax)
min_temp.configure(text=tempmin)
lable_lon.configure(text=longtitude)
lable_lat.configure(text=latitude)
lable_country.configure(text=country)
lable_citi.configure(text=citi)
# Search Bar and Button
city_nameButton = Button(root, text = "Search", command=city_name)
city_nameButton.grid(row=1, column=1, padx=5, stick=W+E+N+S)
# Country Names and Coordinates
lable_citi = Label(root, text="...", width=0, bg='white', font=("bold", 15))
lable_citi.place(x=10, y=63)
lable_country = Label(root, text="...", width=0, bg='white', font=("bold", 15))
lable_country.place(x=135, y=63)
lable_lon = Label(root, text="...", width=0, bg='white', font=("Helvetica", 15))
lable_lon.place(x=25, y=95)
lable_lat = Label(root, text="...", width=0, bg='white', font=("Helvetica", 15))
lable_lat.place(x=95, y=95)
# Current Temperature
lable_temp = Label(root, text="...", width=0, bg='white', font=("Helvetica", 110), fg='black')
lable_temp.place(x=18, y=220)
# Other temperature details
humi = Label(root, text="Humidity: ", width=0, bg='white', font=("bold", 15))
humi.place(x=3, y=400)
lable_humidity = Label(root, text="...", width=0, bg='white', font=("bold", 15))
lable_humidity.place(x=107, y=400)
maxi = Label(root, text="Max. Temp.: ", width=0, bg='white', font=("bold", 15))
maxi.place(x=3, y=430)
max_temp = Label(root, text="...", width=0, bg='white', font=("bold", 15))
max_temp.place(x=128, y=430)
mini = Label(root, text="Min. Temp.: ", width=0, bg='white', font=("bold", 15))
mini.place(x=3, y=460)
min_temp = Label(root, text="...", width=0, bg='white', font=("bold", 15))
min_temp.place(x=128, y=460)
# Note
note = Label(root, text="All temperatures in degree celsius", bg='white', font=("italic", 10))
note.place(x=95, y=495)
root.mainloop()