from tkinter import *
import requests
api_key = "4256b3de394a56a86ee35e43af6f5c2e"
nome_cidade1 = "London"
data = requests.get(
f"https://api.openweathermap.org/data/2.5/weather?q={nome_cidade1}"
f"&units=metric&APPID={api_key}"
)
info = data.json()
temp_min = info['main']['temp_min']
temp_max = info['main']['temp_max']
root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Classificar a Temperatura")
titulo = Label(
root,
text="Classificar a Temperatura",
font=("Arial", 24, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.2, rely=0.05)
lb_min = Label(
text="Temperatura Mínima (°C):",
font=("Helvetica", 12, "bold"),
bg="#103030",
fg="white"
)
lb_min.place(relx=0.05, rely=0.25)
min_resultado = Label(
text=temp_min,
font=("Helvetica", 14, "bold"),
fg="cyan",
bg="#103030"
)
min_resultado.place(relx=0.35, rely=0.25)
lb_max = Label(
text="Temperatura Máxima (°C):",
font=("Helvetica", 12, "bold"),
bg="#103030",
fg="white"
)
lb_max.place(relx=0.05, rely=0.35)
max_resultado = Label(
text=temp_max,
font=("Helvetica", 14, "bold"),
fg="red",
bg="#103030"
)
max_resultado.place(relx=0.35, rely=0.35)
frame_scroll = Frame(root)
frame_scroll.place(relx=0.05, rely=0.65, relwidth=0.9, relheight=0.3)
scrollbar = Scrollbar(frame_scroll)
scrollbar.pack(side=RIGHT, fill=Y)
resultado_texto = Text(
frame_scroll,
font=("Arial", 12, "bold"),
bg="#cfe2f3",
wrap="word",
yscrollcommand=scrollbar.set
)
resultado_texto.pack(side=LEFT, fill=BOTH, expand=True)
scrollbar.config(command=resultado_texto.yview)
# Tags de cor
resultado_texto.tag_configure("muito_frio", foreground="#003399")
resultado_texto.tag_configure("frio", foreground="#3366cc")
resultado_texto.tag_configure("moderado", foreground="#008800")
resultado_texto.tag_configure("quente", foreground="#ff7700")
resultado_texto.tag_configure("muito_quente", foreground="#cc0000")
def limpar():
resultado_texto.delete("1.0", END)
def app():
resultado_texto.delete("1.0", END)
temperatura = temp_max
if temperatura <= 5:
nivel = "muito_frio"
mensagem = (
"Classificação: Muito frio\n\n"
"Conselho: Agasalha-te muito bem! Usa cachecol, gorro e luvas."
)
elif temperatura <= 14:
nivel = "frio"
mensagem = (
"Classificação: Frio\n\n"
"Conselho: Leva um bom casaco, pois pode ficar desconfortável."
)
elif temperatura <= 22:
nivel = "moderado"
mensagem = (
"Classificação: Temperatura moderada\n\n"
"Conselho: Tempo agradável. Aproveita para passear!"
)
elif temperatura <= 29:
nivel = "quente"
mensagem = (
"Classificação: Um pouco quente\n\n"
"Conselho: Bebe água e evita exposição ao sol."
)
else:
nivel = "muito_quente"
mensagem = (
"Classificação: Muito quente\n\n"
"Conselho: Mantém-te hidratado, procura sombra e usa protetor solar."
)
resultado_texto.insert(END, mensagem, nivel)
but1 = Button(
root,
text="Classificar",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=app
)
but1.place(relx=0.1, rely=0.5, relwidth=0.25, relheight=0.1)
but_limpar = Button(
root,
text="Limpar",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=limpar
)
but_limpar.place(relx=0.4, rely=0.5, relwidth=0.25, relheight=0.1)
but_sair = Button(
root,
text="Sair",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=root.destroy
)
but_sair.place(relx=0.7, rely=0.5, relwidth=0.25, relheight=0.1)
root.mainloop()