MaselliSimulatorApp/utils.py

84 lines
2.9 KiB
Python

"""
Utilidades comunes para el proyecto
"""
import tkinter as tk
from datetime import datetime
import os
class Utils:
@staticmethod
def log_message(log_widget, message):
"""Escribe un mensaje con timestamp en el widget de log especificado"""
if log_widget:
log_widget.configure(state=tk.NORMAL)
timestamp = datetime.now().strftime('%H:%M:%S')
log_widget.insert(tk.END, f"[{timestamp}] {message}\n")
log_widget.see(tk.END)
log_widget.configure(state=tk.DISABLED)
@staticmethod
def load_icon(root):
"""Intenta cargar un icono para la ventana"""
icon_loaded = False
for icon_file in ['icon.png', 'icon.ico', 'icon.gif']:
if os.path.exists(icon_file):
try:
if icon_file.endswith('.ico'):
root.iconbitmap(icon_file)
else:
icon = tk.PhotoImage(file=icon_file)
root.iconphoto(True, icon)
icon_loaded = True
break
except Exception as e:
print(f"No se pudo cargar {icon_file}: {e}")
if not icon_loaded:
print("No se encontró ningún archivo de icono (icon.png, icon.ico, icon.gif)")
@staticmethod
def create_csv_filename(prefix="maselli"):
"""Crea un nombre de archivo CSV con timestamp"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
return f"{prefix}_{timestamp}.csv"
@staticmethod
def validate_number_entry(value, min_val=None, max_val=None, is_float=True):
"""Valida que una entrada sea un número válido dentro del rango especificado"""
try:
num = float(value) if is_float else int(value)
if min_val is not None and num < min_val:
return False
if max_val is not None and num > max_val:
return False
return True
except ValueError:
return False
@staticmethod
def set_widgets_state(widgets, state):
"""Establece el estado de múltiples widgets"""
for widget in widgets:
try:
widget.config(state=state)
except:
pass # Algunos widgets pueden no tener la propiedad state
@staticmethod
def format_brix_display(brix_value):
"""Formatea un valor Brix para mostrar"""
return f"{brix_value:.3f} Brix"
@staticmethod
def format_ma_display(ma_value):
"""Formatea un valor mA para mostrar"""
return f"{ma_value:.3f} mA"
@staticmethod
def clear_graph_data(*data_containers):
"""Limpia los contenedores de datos del gráfico"""
for container in data_containers:
if hasattr(container, 'clear'):
container.clear()