Per soddisfare la tua richiesta, creeremo un'applicazione con un'interfaccia grafica professionale utilizzando Python e la libreria tkinter. L'interfaccia includerà:
- Campi di input per modificare uno qualsiasi dei sei valori (altezza complessiva, larghezza complessiva, ecc.).
- Textbox per definire i limiti del piatto di stampa (ad esempio, larghezza massima, altezza massima, ecc.).
- Validazione per assicurarsi che i valori inseriti siano validi e rispettino i limiti del piatto di stampa.
- Aggiornamento dinamico delle dimensioni calcolate in base al rapporto aureo.
- Design professionale con etichette chiare, pulsanti e un layout organizzato.
Implementazione in Python con tkinter
Ecco il codice completo per l'applicazione:
import tkinter as tk
from tkinter import ttk, messagebox
import math
# Costante del rapporto aureo
PHI = (1 + math.sqrt(5)) / 2 # ≈ 1.618033988749895
class LampadaApp:
def __init__(self, root):
self.root = root
self.root.title("Calcolatore Lampada - Rapporto Aureo")
self.root.geometry("600x700")
self.root.resizable(False, False)
# Variabili per i limiti del piatto di stampa
self.limite_larghezza = tk.DoubleVar(value=200.0) # Default: 200 mm
self.limite_altezza = tk.DoubleVar(value=300.0) # Default: 300 mm
# Variabili per le dimensioni
self.dimensioni_vars = {
"altezza_complessiva": tk.DoubleVar(value=100.0),
"larghezza_complessiva": tk.DoubleVar(value=61.8),
"altezza_paralume": tk.DoubleVar(value=61.8),
"larghezza_paralume": tk.DoubleVar(value=38.2),
"altezza_base": tk.DoubleVar(value=38.2),
"larghezza_base": tk.DoubleVar(value=23.6)
}
# Variabile per tracciare quale dimensione è stata modificata
self.valore_modificato = tk.StringVar(value="altezza_complessiva")
# Creazione dell'interfaccia
self.crea_interfaccia()
def crea_interfaccia(self):
# Stile professionale
style = ttk.Style()
style.configure("TLabel", font=("Helvetica", 10))
style.configure("TButton", font=("Helvetica", 10, "bold"))
style.configure("TEntry", font=("Helvetica", 10))
# Frame per i limiti del piatto di stampa
frame_limiti = ttk.LabelFrame(self.root, text="Limiti Piatto di Stampa (mm)", padding=10)
frame_limiti.pack(padx=10, pady=10, fill="x")
ttk.Label(frame_limiti, text="Larghezza Massima:").grid(row=0, column=0, padx=5, pady=5, sticky="e")
ttk.Entry(frame_limiti, textvariable=self.limite_larghezza, width=10).grid(row=0, column=1, padx=5, pady=5)
ttk.Label(frame_limiti, text="Altezza Massima:").grid(row=1, column=0, padx=5, pady=5, sticky="e")
ttk.Entry(frame_limiti, textvariable=self.limite_altezza, width=10).grid(row=1, column=1, padx=5, pady=5)
# Frame per la selezione del valore da modificare
frame_selezione = ttk.LabelFrame(self.root, text="Seleziona Valore da Modificare", padding=10)
frame_selezione.pack(padx=10, pady=10, fill="x")
valori = [
("Altezza Complessiva", "altezza_complessiva"),
("Larghezza Complessiva", "larghezza_complessiva"),
("Altezza Paralume", "altezza_paralume"),
("Larghezza Paralume", "larghezza_paralume"),
("Altezza Base", "altezza_base"),
("Larghezza Base", "larghezza_base")
]
for i, (etichetta, valore) in enumerate(valori):
ttk.Radiobutton(frame_selezione, text=etichetta, variable=self.valore_modificato, value=valore).grid(row=i//2, column=i%2, padx=5, pady=5, sticky="w")
# Frame per l'input del valore
frame_input = ttk.LabelFrame(self.root, text="Inserisci Valore (mm)", padding=10)
frame_input.pack(padx=10, pady=10, fill="x")
self.input_valore = tk.DoubleVar(value=100.0)
ttk.Entry(frame_input, textvariable=self.input_valore, width=15).grid(row=0, column=0, padx=5, pady=5)
ttk.Button(frame_input, text="Calcola", command=self.calcola_dimensioni).grid(row=0, column=1, padx=5, pady=5)
# Frame per i risultati
frame_risultati = ttk.LabelFrame(self.root, text="Dimensioni Calcolate (mm)", padding=10)
frame_risultati.pack(padx=10, pady=10, fill="x")
self.risultati_labels = {}
for i, (chiave, var) in enumerate(self.dimensioni_vars.items()):
etichetta = chiave.replace("_", " ").title()
ttk.Label(frame_risultati, text=f"{etichetta}:").grid(row=i, column=0, padx=5, pady=5, sticky="e")
ttk.Label(frame_risultati, textvariable=var, width=15).grid(row=i, column=1, padx=5, pady=5, sticky="w")
self.risultati_labels[chiave] = var
def calcola_dimensioni(self):
try:
valore = self.input_valore.get()
tipo_valore = self.valore_modificato.get()
limite_larghezza = self.limite_larghezza.get()
limite_altezza = self.limite_altezza.get()
if valore <= 0:
raise ValueError("Il valore deve essere maggiore di zero.")
if limite_larghezza <= 0 or limite_altezza <= 0:
raise ValueError("I limiti del piatto di stampa devono essere maggiori di zero.")
# Calcola le dimensioni
dimensioni = self.calcola_dimensioni_auree(valore, tipo_valore)
# Verifica i limiti del piatto di stampa
if dimensioni["altezza_complessiva"] > limite_altezza:
raise ValueError(f"L'altezza complessiva ({dimensioni['altezza_complessiva']:.2f} mm) supera il limite di {limite_altezza:.2f} mm.")
if dimensioni["larghezza_complessiva"] > limite_larghezza:
raise ValueError(f"La larghezza complessiva ({dimensioni['larghezza_complessiva']:.2f} mm) supera il limite di {limite_larghezza:.2f} mm.")
# Aggiorna le variabili dei risultati
for chiave, valore in dimensioni.items():
self.risultati_labels[chiave].set(round(valore, 2))
except ValueError as e:
messagebox.showerror("Errore", str(e))
except Exception as e:
messagebox.showerror("Errore", f"Errore imprevisto: {str(e)}")
def calcola_dimensioni_auree(self, valore, tipo_valore):
"""
Calcola tutte le dimensioni della lampada dato un valore iniziale.
Parametri:
- valore: il valore numerico inserito dall'utente
- tipo_valore: stringa che indica quale dimensione è stata modificata
"""
if tipo_valore == "altezza_complessiva":
h_tot = valore
w_tot = h_tot / PHI
w_p = w_tot
h_p = w_p * PHI
h_b = h_tot - h_p
w_b = h_b / PHI
elif tipo_valore == "larghezza_complessiva":
w_tot = valore
h_tot = w_tot * PHI
w_p = w_tot
h_p = w_p * PHI
h_b = h_tot - h_p
w_b = h_b / PHI
elif tipo_valore == "altezza_paralume":
h_p = valore
w_p = h_p / PHI
w_tot = w_p
h_tot = w_tot * PHI
h_b = h_tot - h_p
w_b = h_b / PHI
elif tipo_valore == "larghezza_paralume":
w_p = valore
h_p = w_p * PHI
w_tot = w_p
h_tot = w_tot * PHI
h_b = h_tot - h_p
w_b = h_b / PHI
elif tipo_valore == "altezza_base":
h_b = valore
w_b = h_b / PHI
w_tot = w_b * PHI # Assumiamo che w_tot sia legato a w_b tramite PHI
h_tot = w_tot * PHI
h_p = h_tot - h_b
w_p = h_p / PHI
elif tipo_valore == "larghezza_base":
w_b = valore
h_b = w_b * PHI
w_tot = w_b * PHI # Assumiamo che w_tot sia legato a w_b tramite PHI
h_tot = w_tot * PHI
h_p = h_tot - h_b
w_p = h_p / PHI
else:
raise ValueError("Tipo di valore non valido.")
# Verifica che tutte le dimensioni siano positive
if any(x <= 0 for x in [h_tot, w_tot, h_p, w_p, h_b, w_b]):
raise ValueError("Il valore inserito produce dimensioni non valide (negative o zero).")
# Restituisci un dizionario con tutte le dimensioni
return {
"altezza_complessiva": h_tot,
"larghezza_complessiva": w_tot,
"altezza_paralume": h_p,
"larghezza_paralume": w_p,
"altezza_base": h_b,
"larghezza_base": w_b
}
if __name__ == "__main__":
root = tk.Tk()
app = LampadaApp(root)
root.mainloop()