import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class SpinboxDemo(tk.Tk):
def __init__(self):
super().__init__()
# Configurar la ventana principal
self.title("Ejemplos de Widget Spinbox")
self.geometry("400x500")
self.configure(bg='#f0f0f0')
# Crear y empaquetar la etiqueta del título
title = ttk.Label(
self,
text="Ejemplos de Spinbox",
font=('Helvetica', 16, 'bold')
)
title.pack(pady=20)
# Crear frame para el spinbox básico
self.create_numeric_spinbox()
# Crear frame para el spinbox de texto
self.create_text_spinbox()
# Crear frame para el spinbox personalizado
self.create_custom_spinbox()
# Crear frame para el spinbox de validación
self.create_validated_spinbox()
def create_numeric_spinbox(self):
"""Crea un frame con un spinbox numérico básico"""
frame = ttk.LabelFrame(self, text="Spinbox Numérico Básico")
frame.pack(padx=10, pady=10, fill="x")
# Spinbox numérico básico
self.num_spinbox = ttk.Spinbox(
frame,
from_=0,
to=100,
increment=5,
command=self.on_numeric_change
)
self.num_spinbox.pack(padx=10, pady=10)
# Etiqueta para mostrar el valor seleccionado
self.num_value_label = ttk.Label(frame, text="Seleccionado: 0")
self.num_value_label.pack(pady=5)
def create_text_spinbox(self):
"""Crea un frame con un spinbox basado en texto"""
frame = ttk.LabelFrame(self, text="Spinbox de Texto")
frame.pack(padx=10, pady=10, fill="x")
# Spinbox basado en texto
days = ('Lunes', 'Martes', 'Miércoles', 'Jueves',
'Viernes', 'Sábado', 'Domingo')
self.text_spinbox = ttk.Spinbox(
frame,
values=days,
wrap=True,
command=self.on_day_change
)
self.text_spinbox.pack(padx=10, pady=10)
# Etiqueta para mostrar el día seleccionado
self.day_label = ttk.Label(frame, text="Seleccionado: Lunes")
self.day_label.pack(pady=5)
def create_custom_spinbox(self):
"""Crea un frame con un spinbox personalizado"""
frame = ttk.LabelFrame(self, text="Spinbox Personalizado")
frame.pack(padx=10, pady=10, fill="x")
# Spinbox personalizado con generación de color aleatorio
self.color_spinbox = ttk.Spinbox(
frame,
from_=0,
to=255,
increment=5,
command=self.update_color
)
self.color_spinbox.pack(padx=10, pady=10)
# Etiqueta de vista previa de color
self.color_label = tk.Label(
frame,
text="Vista previa de color",
width=20,
height=2
)
self.color_label.pack(pady=5)
def create_validated_spinbox(self):
"""Crea un frame con un spinbox validado"""
frame = ttk.LabelFrame(self, text="Spinbox Validado (Solo Números Pares)")
frame.pack(padx=10, pady=10, fill="x")
# Función de validación
vcmd = (self.register(self.validate_even), '%P')
# Spinbox validado
self.valid_spinbox = ttk.Spinbox(
frame,
from_=0,
to=100,
increment=2,
validate='key',
validatecommand=vcmd
)
self.valid_spinbox.pack(padx=10, pady=10)
def on_numeric_change(self):
"""Manejador para cambios en el spinbox numérico"""
value = self.num_spinbox.get()
self.num_value_label.config(text=f"Seleccionado: {value}")
def on_day_change(self):
"""Manejador para cambios en el spinbox de día"""
day = self.text_spinbox.get()
self.day_label.config(text=f"Seleccionado: {day}")
def update_color(self):
"""Manejador para cambios en el spinbox de color"""
try:
value = int(self.color_spinbox.get())
# Crea un gradiente de color de rojo a azul
color = f'#{value:02x}00{255-value:02x}'
self.color_label.config(bg=color)
except ValueError:
pass
def validate_even(self, value):
"""Función de validación solo para números pares"""
if value == "":
return True
try:
num = int(value)
return num % 2 == 0
except ValueError:
return False
if __name__ == "__main__":
app = SpinboxDemo()
app.mainloop()