Clima en el panel izquierdo

This commit is contained in:
Andrés Moran 2024-12-11 15:37:15 +01:00
parent 5b8f4f45b7
commit 6ab1ca03d3
5 changed files with 63 additions and 5 deletions

Binary file not shown.

View File

@ -1,15 +1,19 @@
import tkinter as tk
import threading
import requests
from PIL import Image, ImageTk
from io import BytesIO
from app import scraping
class PanelIzquierdo:
def __init__(self, frame, text_widget):
# Asociar el frame del panel izquierdo
# Configuración del panel
self.frame = frame
self.frame.configure(bg="lightblue", width=200)
self.frame.configure(bg="lightblue", width=300)
self.frame.grid_propagate(False)
self.frame.columnconfigure(0, weight=1)
# Botón para iniciar scraping (ya existente)
boton_scrapping = tk.Button(
frame,
text="Iniciar Scrapping",
@ -18,4 +22,58 @@ class PanelIzquierdo:
args=("https://www.amazon.es/", text_widget)
).start()
)
boton_scrapping.pack(pady=5)
boton_scrapping.pack(pady=5)
# Clima - Título
self.weather_title = tk.Label(
frame, text="Clima Actual", bg="lightblue", font=("Helvetica", 14, "bold"), fg="navy"
)
self.weather_title.pack(pady=10)
# Clima - Ícono
self.weather_icon = tk.Label(frame, bg="lightblue")
self.weather_icon.pack()
# Clima - Información
self.weather_label = tk.Label(
frame, text="Cargando clima...", bg="lightblue", font=("Helvetica", 10, "bold"), fg="black"
)
self.weather_label.pack(pady=5)
# Variables para el clima
self.api_key = '2f79c6f35c48e876bceae7fa7f4f4735'
self.city = 'Javea' # Cambia por la ciudad que desees
self.update_weather()
def update_weather(self):
"""Actualiza la información del clima utilizando un hilo."""
def fetch_weather():
try:
url = f'http://api.openweathermap.org/data/2.5/weather?q={self.city}&appid={self.api_key}&units=metric&lang=es'
response = requests.get(url)
response.raise_for_status()
data = response.json()
# Información del clima
temp = data['main']['temp']
weather = data['weather'][0]['description']
icon_code = data['weather'][0]['icon']
# Actualiza el texto del clima
self.weather_label.config(
text=f"{self.city}:\n{temp}°C, {weather.capitalize()}"
)
# Descarga y muestra el ícono del clima
icon_url = f"http://openweathermap.org/img/wn/{icon_code}@2x.png"
icon_response = requests.get(icon_url)
icon_image = Image.open(BytesIO(icon_response.content))
icon_photo = ImageTk.PhotoImage(icon_image)
self.weather_icon.config(image=icon_photo)
self.weather_icon.image = icon_photo # Referencia para evitar que se elimine
except Exception as e:
self.weather_label.config(text=f"Error al obtener el clima.")
print(f"Error al obtener el clima: {e}")
threading.Thread(target=fetch_weather, daemon=True).start()

View File

@ -64,7 +64,7 @@ menu_bar.add_cascade(label="Ayuda", menu=help_menu)
root.config(menu=menu_bar)
# Crear los frames laterales y el central
frame_izquierdo = tk.Frame(root, bg="lightblue", width=200)
frame_izquierdo = tk.Frame(root, bg="lightblue", width=300)
frame_central = tk.Frame(root, bg="white")
frame_derecho = tk.Frame(root, bg="lightgreen", width=200)