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): # Configuración del panel self.frame = frame 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", command=lambda: threading.Thread( target=scraping.iniciar_scraping_y_insercion, args=("https://www.amazon.es/", text_widget) ).start() ) 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()