import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class SpinboxDemo(tk.Tk):
def __init__(self):
super().__init__()
# Configure the main window
self.title("Spinbox Widget Examples")
self.geometry("400x500")
self.configure(bg='#f0f0f0')
# Create and pack the title label
title = ttk.Label(
self,
text="Spinbox Examples",
font=('Helvetica', 16, 'bold')
)
title.pack(pady=20)
# Create frame for basic spinbox
self.create_numeric_spinbox()
# Create frame for text spinbox
self.create_text_spinbox()
# Create frame for custom spinbox
self.create_custom_spinbox()
# Create frame for validation spinbox
self.create_validated_spinbox()
def create_numeric_spinbox(self):
"""Creates a frame with a basic numeric spinbox"""
frame = ttk.LabelFrame(self, text="Basic Numeric Spinbox")
frame.pack(padx=10, pady=10, fill="x")
# Basic numeric spinbox
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)
# Label to show selected value
self.num_value_label = ttk.Label(frame, text="Selected: 0")
self.num_value_label.pack(pady=5)
def create_text_spinbox(self):
"""Creates a frame with a text-based spinbox"""
frame = ttk.LabelFrame(self, text="Text Spinbox")
frame.pack(padx=10, pady=10, fill="x")
# Text-based spinbox
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday')
self.text_spinbox = ttk.Spinbox(
frame,
values=days,
wrap=True,
command=self.on_day_change
)
self.text_spinbox.pack(padx=10, pady=10)
# Label to show selected day
self.day_label = ttk.Label(frame, text="Selected: Monday")
self.day_label.pack(pady=5)
def create_custom_spinbox(self):
"""Creates a frame with a custom-styled spinbox"""
frame = ttk.LabelFrame(self, text="Custom Spinbox")
frame.pack(padx=10, pady=10, fill="x")
# Custom spinbox with random color generation
self.color_spinbox = ttk.Spinbox(
frame,
from_=0,
to=255,
increment=5,
command=self.update_color
)
self.color_spinbox.pack(padx=10, pady=10)
# Color display label
self.color_label = tk.Label(
frame,
text="Color Preview",
width=20,
height=2
)
self.color_label.pack(pady=5)
def create_validated_spinbox(self):
"""Creates a frame with a validated spinbox"""
frame = ttk.LabelFrame(self, text="Validated Spinbox (Even Numbers Only)")
frame.pack(padx=10, pady=10, fill="x")
# Validation function
vcmd = (self.register(self.validate_even), '%P')
# Validated spinbox
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):
"""Handler for numeric spinbox changes"""
value = self.num_spinbox.get()
self.num_value_label.config(text=f"Selected: {value}")
def on_day_change(self):
"""Handler for day spinbox changes"""
day = self.text_spinbox.get()
self.day_label.config(text=f"Selected: {day}")
def update_color(self):
"""Handler for color spinbox changes"""
try:
value = int(self.color_spinbox.get())
# Create a color gradient from red to blue
color = f'#{value:02x}00{255-value:02x}'
self.color_label.config(bg=color)
except ValueError:
pass
def validate_even(self, value):
"""Validation function for even numbers only"""
if value == "":
return True
try:
num = int(value)
return num % 2 == 0
except ValueError:
return False
if __name__ == "__main__":
app = SpinboxDemo()
app.mainloop()