From 7c394f0b657d6f1f24b6106cb300a1dd9b4300fb Mon Sep 17 00:00:00 2001 From: Filippo Lizzi Date: Fri, 26 May 2023 21:43:53 +0200 Subject: [PATCH] Added celsius, fahrenheit and kelvin units, centered window and added invalid city error --- main.py | 72 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index 43ccfac..65080ec 100644 --- a/main.py +++ b/main.py @@ -1,44 +1,74 @@ import tkinter as tk import requests import time +import screeninfo +def getWeather(root): + city = cityEntry.get() + unit = "standard" + symbol = "K" -def getWeather(canvas): - city = textField.get() - api = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=f01e80368f05c66b03425d3f08ab1a1c" + if unitsVariable.get() == "Celsius": + unit = "metric" + symbol = "°C" + elif unitsVariable.get() == "Fahrenheit": + unit = "imperial" + symbol = "°F" + else: + unit = "standard" + symbol = "K" + + api = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=f01e80368f05c66b03425d3f08ab1a1c" + "&units=" + unit json_data = requests.get(api).json() + + if json_data["cod"] == "404": + cityEntry.delete(0, tk.END) + cityEntry.insert(0, "Invalid city") + + return + condition = json_data['weather'][0]['main'] - temp = int(json_data['main']['temp'] - 273.15) - min_temp = int(json_data['main']['temp_min'] - 273.15) - max_temp = int(json_data['main']['temp_max'] - 273.15) + temp = int(json_data['main']['temp']) + min_temp = int(json_data['main']['temp_min']) + max_temp = int(json_data['main']['temp_max']) pressure = json_data['main']['pressure'] humidity = json_data['main']['humidity'] wind = json_data['wind']['speed'] sunrise = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunrise'] - 21600)) sunset = time.strftime('%I:%M:%S', time.gmtime(json_data['sys']['sunset'] - 21600)) - final_info = condition + "\n" + str(temp) + "°C" - final_data = "\n" + "Min Temp: " + str(min_temp) + "°C" + "\n" + "Max Temp: " + str( - max_temp) + "°C" + "\n" + "Pressure: " + str(pressure) + "\n" + "Humidity: " + str( + final_info = condition + "\n" + str(temp) + symbol + final_data = "\n" + "Min Temp: " + str(min_temp) + symbol + "\n" + "Max Temp: " + str( + max_temp) + symbol + "\n" + "Pressure: " + str(pressure) + "\n" + "Humidity: " + str( humidity) + "\n" + "Wind Speed: " + str(wind) + "\n" + "Sunrise: " + sunrise + "\n" + "Sunset: " + sunset label1.config(text=final_info) label2.config(text=final_data) +screenWidth = screeninfo.get_monitors()[0].width +screenHeight = screeninfo.get_monitors()[0].height + +root = tk.Tk() +root.geometry(f"600x500+{int(screenWidth/2-600/2)}+{int(screenHeight/2-500/2)}") +root.title("Weather App") +root.resizable(False, False) + +cityLabelFrame = tk.LabelFrame(root, text="Enter a city") +cityLabelFrame.pack(padx=20) -canvas = tk.Tk() -canvas.geometry("600x500") -canvas.title("Weather App") -f = ("poppins", 15, "bold") -t = ("poppins", 35, "bold") +cityEntry = tk.Entry(cityLabelFrame, justify='center', width=20, font=("poppins", 35, "bold")) +cityEntry.pack(padx=20, pady=20) +cityEntry.focus() +cityEntry.bind('', getWeather) -textField = tk.Entry(canvas, justify='center', width=20, font=t) -textField.pack(pady=20) -textField.focus() -textField.bind('', getWeather) +unitsVariable = tk.StringVar() +units = ["Celsius", "Fahrenheit", "Kelvin"] +optionMenu = tk.OptionMenu(root, unitsVariable, *units) +optionMenu.pack(fill=tk.X, padx=20) +unitsVariable.set("Celsius") -label1 = tk.Label(canvas, font=t) +label1 = tk.Label(root, font=("poppins", 35, "bold")) label1.pack() -label2 = tk.Label(canvas, font=f) +label2 = tk.Label(root, font=("poppins", 15, "bold")) label2.pack() -canvas.mainloop() \ No newline at end of file +root.mainloop() \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy