-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathweatherapp.py
More file actions
50 lines (39 loc) · 1.25 KB
/
weatherapp.py
File metadata and controls
50 lines (39 loc) · 1.25 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
# Importing required modules
import requests
from bs4 import BeautifulSoup
import tkinter as tk
# Creating function to get temperature
def get_temp():
# Taking City You Want to Check Temperature
city = city_entry.get()
# Storing City You Want to Check Temperature
search = "weather in" + city
# Searching it on google
url = f"https://www.google.com/search?&q={search}"
# Sending and Receiving Requests
r = requests.get(url)
# Scrape the temperature from the search results
s = BeautifulSoup(r.text, "html.parser")
# Storing details
update = s.find("div", class_="BNeawe").text
# Display the temperature
temperature_label.config(text="Temperature in " + city + " is: " + update)
# Creating the main window
root = tk.Tk()
root.geometry('200x200')
# Creating label for city
city_label = tk.Label(root, text="City: ")
# Creating entry widget for city
city_entry = tk.Entry(root)
# Creating button to get temperature
get_temperature_button = tk.Button(
root, text="Get Temperature", command=get_temp)
# Displaying the temperature
temperature_label = tk.Label(root)
# Positioning the widgets
city_label.pack()
city_entry.pack()
get_temperature_button.pack()
temperature_label.pack()
# Starting main loop
root.mainloop()