|
| 1 | +# Importing required modules |
1 | 2 | import requests
|
2 | 3 | from bs4 import BeautifulSoup
|
| 4 | +import tkinter as tk |
3 | 5 |
|
4 |
| -# Taking City You Want to Check Temperaature |
5 |
| -city = input("Enter City : ") |
6 |
| -# Storing City You Want to Check Temperaature |
7 |
| -search = "weather in" + city |
8 |
| -# Searching it on google |
9 |
| -url = f"https://www.google.com/search?&q={search}" |
10 |
| -# Sending and Receiving Requests |
11 |
| -r = requests.get(url) |
12 |
| -# Scraping Details |
13 |
| -s = BeautifulSoup(r.text, "html.parser") |
14 |
| -# Storing Details |
15 |
| -update = s.find("div", class_="BNeawe").text |
16 |
| -# Printing Details |
17 |
| -print("Temperature in " + city + " is: " + update) |
| 6 | +# Creating function to get temperature |
| 7 | +def get_temp(): |
| 8 | + # Taking City You Want to Check Temperature |
| 9 | + city = city_entry.get() |
| 10 | + # Storing City You Want to Check Temperature |
| 11 | + search = "weather in" + city |
| 12 | + # Searching it on google |
| 13 | + url = f"https://www.google.com/search?&q={search}" |
| 14 | + # Sending and Receiving Requests |
| 15 | + r = requests.get(url) |
| 16 | + # Scrape the temperature from the search results |
| 17 | + s = BeautifulSoup(r.text, "html.parser") |
| 18 | + # Storing details |
| 19 | + update = s.find("div", class_="BNeawe").text |
| 20 | + # Display the temperature |
| 21 | + temperature_label.config(text="Temperature in " + city + " is: " + update) |
| 22 | + |
| 23 | + |
| 24 | +# Creating the main window |
| 25 | +root = tk.Tk() |
| 26 | +root.geometry('200x200') |
| 27 | + |
| 28 | +# Creating label for city |
| 29 | +city_label = tk.Label(root, text="City: ") |
| 30 | + |
| 31 | +# Creating entry widget for city |
| 32 | +city_entry = tk.Entry(root) |
| 33 | + |
| 34 | +# Creating button to get temperature |
| 35 | +get_temperature_button = tk.Button( |
| 36 | + root, text="Get Temperature", command=get_temp) |
| 37 | + |
| 38 | +# Displaying the temperature |
| 39 | +temperature_label = tk.Label(root) |
| 40 | + |
| 41 | +# Positioning the widgets |
| 42 | +city_label.pack() |
| 43 | +city_entry.pack() |
| 44 | +get_temperature_button.pack() |
| 45 | +temperature_label.pack() |
| 46 | + |
| 47 | +# Starting main loop |
| 48 | +root.mainloop() |
0 commit comments