Steve
Steve

Reputation: 5

How do you make an animation with the title of a Tkinter window?

This is a bit of an eccentric question, but I would like an answer.

All I want to do is make an animation with the title of a window, by which I mean slowly deleting each letter of the title until it's blank. This is the example of the code in question.

def Clearcommand():
for letter in the_window.title():
    the_window.title(the_window.title()[:len(the_window.title())-1])

I was hoping that this code – when called by the button it's attached to – would delete a single letter off the end until the title is cleared.

The irritating part is that it doesn't break the code in question, it just seems to update the title once the 'Clearcommand' function ends.

Is there a way to force a window update that I'm overseeing, or am I going about this the wrong way?


Example code if you want to test it out:

from Tkinter import *

window = Tk()
window.title('This title contains so much guff.')

def Clearcommand():
    for letter in window.title():
        window.title(window.title()[:len(window.title())-1])

Clearbutton = Button(window,text = 'Clear Title',command = Clearcommand)
Clearbutton.pack(padx = 100)

window.mainloop()

Upvotes: 0

Views: 199

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

Do it like you do any other animation in tkinter: with after:

def Clearcommand():
    title = the_window.title()[:-1]
    the_window.title(title)
    if len(title) > 0:
        the_window.after(1000, Clearcommand)

Upvotes: 1

Related Questions