Create 3D Text Animations In Python Tkinter

How To Create an Animated Text in Python Using Tkinter

How to Create 3D Text Animation In Python Tkinter


In this Python Tutorial we will see How to Create an animated text effects using Python and Tkinter library.
The text appears with a growth animation, cycles through vibrant colors, and maintains subtle movement patterns while displaying layered shadow effects for depth and impact.

What We Are Gonna Use In This Project:

- Python Programming Language.
- Tkinter (GUI).
- VS Editor.






Project Source Code:




import tkinter as tk
import math
import time
from tkinter import font

class TextAnimation(tk.Canvas):
"""
A smooth animated text effect that creates a glowing, colorful text
with shadow layers and gentle rotation.
"""
def __init__(self, master, text, **kwargs):
super().__init__(master, **kwargs)
self.text = text
# Animation state variables
self.scale = 0.1 # Text starts small and grows
self.is_growing = True # Flag to control growth phase
self.rotation_angle = 0 # Current rotation angle
self.color_time = 0 # Time variable for color cycling
# Animation settings
self.grow_speed = 0.02 # How fast text grows (0.01 = slow, 0.05 = fast)
self.rotation_speed = 0.1 # How fast text rotates
self.color_speed = 0.03 # How fast colors change
self.shadow_layers = 30 # Number of shadow/glow layers
# Colors for the gradient effect (RGB values)
self.colors = [
(255, 0, 128), # Hot pink
(255, 102, 0), # Orange
(255, 215, 0), # Gold
(0, 255, 255) # Cyan
]
# List of texts to cycle through when clicked
self.text_options = [
"FIGHT!",
"POWER!",
"EPIC!",
"AWESOME!",
"VICTORY!",
"LEGEND!",
"SUPREME!",
"ULTIMATE!"
]
self.current_text_index = 0
# Set up click event to change text
self.bind("<Button-1>", self.on_click)
# Start the animation loop
self.animate()

def animate(self):
"""
Main animation loop - called repeatedly to update the animation
"""
# Update animation values
self.update_animation_values()
# Clear the canvas and redraw everything
self.delete("all")
self.draw_animated_text()
# Schedule the next frame (20ms delay)
self.after(20, self.animate)
def update_animation_values(self):
"""
Update all the animation variables for smooth movement
"""
# Handle the growing phase when text first appears
if self.is_growing:
self.scale += self.grow_speed
if self.scale >= 1.0:
self.scale = 1.0
self.is_growing = False
# Update rotation and color cycling (always happening)
self.rotation_angle += self.rotation_speed
self.color_time += self.color_speed

def draw_animated_text(self):
"""
Draw the text with all effects: shadows, colors, and rotation
"""
# Get canvas dimensions for centering
width = self.winfo_width()
height = self.winfo_height()
center_x = width // 2
center_y = height // 2
# Calculate font size based on window size and current scale
base_font_size = min(width, height) // 6
current_font_size = int(base_font_size * self.scale)
text_font = font.Font(family="Impact", size=current_font_size, weight="bold")
# Draw shadow layers first (back to front)
self.draw_shadow_layers(center_x, center_y, text_font)
# Draw the main text on top
self.draw_main_text(center_x, center_y, text_font)


def draw_shadow_layers(self, x, y, text_font):
"""
Draw multiple shadow layers
"""
for layer in range(self.shadow_layers, 0, -1):
# Calculate how far back this shadow layer is (0.0 to 1.0)
depth = layer / self.shadow_layers
# Get color for this shadow layer
shadow_color = self.get_gradient_color(depth + self.color_time)
# Make shadow
color_hex = self.rgb_to_hex(shadow_color)
# Calculate shadow offset with gentle wave motion
wave = math.sin(self.rotation_angle + layer * 0.1)
offset_x = layer * 2 + wave * 1.5
offset_y = layer * 2 + wave * 1.5
# Draw this shadow layer
self.create_text(
x + offset_x, y + offset_y,
text=self.text,
font=text_font,
fill=color_hex,
anchor="center"
)


def draw_main_text(self, x, y, text_font):
"""
Draw the main text with bright colors and subtle rotation
"""
# Get the main color
main_color = self.get_gradient_color(self.color_time)
color_hex = self.rgb_to_hex(main_color)
# Add gentle swaying motion (only after growing is complete)
sway = 0
if not self.is_growing:
sway = math.sin(self.rotation_angle * 0.5) * 3
# Draw the main text
self.create_text(
x + sway, y,
text=self.text,
font=text_font,
fill=color_hex,
anchor="center"
)
# Add a bright highlight on top
highlight_color = self.rgb_to_hex((255, 255, 255))
self.create_text(
x + sway - 1, y - 1,
text=self.text,
font=text_font,
fill=highlight_color,
anchor="center"
)


def get_gradient_color(self, position):
"""
Get a color from the gradient based on position (0.0 to 1.0)
Creates smooth color transitions between the defined colors
"""
# Keep position between 0 and 1
position = position % 1.0
# Find which two colors to blend
color_sections = len(self.colors) - 1
scaled_position = position * color_sections
color_index = int(scaled_position)
blend_amount = scaled_position - color_index
# Get the two colors to blend
color1 = self.colors[color_index]
color2 = self.colors[(color_index + 1) % len(self.colors)]
# Blend the colors smoothly
r = int(color1[0] + (color2[0] - color1[0]) * blend_amount)
g = int(color1[1] + (color2[1] - color1[1]) * blend_amount)
b = int(color1[2] + (color2[2] - color1[2]) * blend_amount)
return (r, g, b)


def on_click(self, event):
"""
Handle mouse clicks to change the text
"""
# Move to the next text in the list
self.current_text_index = (self.current_text_index + 1) % len(self.text_options)
self.text = self.text_options[self.current_text_index]
# Reset the growing animation
self.scale = 0.3 # Start a bit bigger for text changes
self.is_growing = True
# Add a little color boost when text changes
self.color_time += 0.5


def rgb_to_hex(self, rgb_color):
"""
Convert RGB color values to hex format for tkinter
rgb_color: tuple of (red, green, blue) values (0-255)
"""
r, g, b = rgb_color
return f"#{r:02x}{g:02x}{b:02x}"




class MainWindow(tk.Tk):
"""
Main application window that contains the text animation
"""
def __init__(self):
super().__init__()
# Window setup
self.title("Smooth Text Animation")
self.geometry("800x400")
self.configure(bg="#1a1a1a") # Dark background
# Create the animated text
self.animation = TextAnimation(
self,
text="FIGHT!",
bg="#1a1a1a", # Match window background
highlightthickness=0 # Remove border
)
# Make the animation fill the entire window
self.animation.pack(fill=tk.BOTH, expand=True)


# Run the application
if __name__ == "__main__":
app = MainWindow()
app.mainloop()




The Final Result:

Python Text Animation With 3D Effects Using Tkinter - 1

Python Text Animation With 3D Effects Using Tkinter - 2

Python Text Animation With 3D Effects Using Tkinter - 3

Python Text Animation With 3D Effects Using Tkinter - 4

Python Text Animation With 3D Effects Using Tkinter - 5

Python Text Animation With 3D Effects Using Tkinter - 6

Python Text Animation With 3D Effects Using Tkinter - 7

Python Text Animation With 3D Effects Using Tkinter - 8









Share this

Related Posts

Latest
Previous
Next Post »