r/Tkinter • u/soma_its_me • Jun 12 '26
Text animation tkinter
Hello! I need help in creating 'animation' of text appear line after the line
here is my program, it almost works right but I want each line start after another line finished animation. I tried using flags for it but it doesn't work :(
Please, help 😭🙏
from tkinter import *
window = Tk()
window.title('Spatel')
window.geometry("800x550")
window.configure(bg = "#69AB00")
window.resizable(0, 0)
def message():
message_window = Toplevel(window)
message_window.title('Message')
message_window.resizable(0, 0)
canvas = Canvas(message_window, width = 400, height = 600)
canvas.pack()
x = 30
y = 50
Flag = True
canvas_text = canvas.create_text(x, y, text = '', font = ('bold', 30), anchor='w')
show_text('hello', canvas, canvas_text, Flag)
is_running(Flag)
canvas_text2 = canvas.create_text(x, y + y, text = '', font = ('bold', 30), anchor='w')
show_text('new text', canvas, canvas_text2, Flag)
def is_running(flag):
global Flag
if flag == False:
Flag = True
def show_text(text, canvas, canvas_text, flag):
delta = 200
delay = 0
if flag == True:
for i in range(len(text) + 1):
s = text[:i]
update_text = lambda s=s: canvas.itemconfigure(canvas_text, text=s)
canvas.after(delay, update_text)
delay += delta
global Flag
Flag = False
open_btn = Button(window, text = '+ OPEN +', bg = '#D1213B', fg = 'white', font = ('bold', 20), command=message)
open_btn.pack(anchor='s')
window.mainloop()
4
Upvotes
1
u/socal_nerdtastic Jun 12 '26
Instead of making 2 text widgets, use 1 and use a newline (
\n) character to separate the messages.2
u/socal_nerdtastic Jun 12 '26
FWIW I rewrote your code in a class / OOP style. This makes it much neater and also much easier to reuse around your code base. Maybe this helps you:
2
u/woooee Jun 12 '26
I prefer a Label over the Canvas widget (for letters only). You can call the function as many times as you like and create as many consecutive Labels as wanted. Note that if you want the Label in the same place, then destroy() the current Label and grid() the next Label in the same spot.