m0.o
m0.o

Reputation: 115

Python Tkinter - Toplevel MoveInAnimation


Is there an option to simply move a Tkinter TopLevel() window on runtime by using an animation? I thought about a smooth moveInAnimation triggered by a button.
Here is some code snippet:

from Tkinter import Toplevel

class MoveInTopLevel(Toplevel):
    '''
    Animated MoveInToplevel.
    '''
    def __init__(self, *args, **kwargs):
        Toplevel.__init__(self, *args, **kwargs)
        self.overrideredirect(1)

    def move_in_from_bottom(self, rootHeight):
        y = rootHeight
        y = max(y-1, 0)
        s = "100x100+0+" + str(y)
        print s

        self.geometry(s)
        self.deiconify()

        if  y > 0:
            self.after(5, self.move_in_from_bottom(y))

Called for example in the mainFunction like this:

window = MoveInTopLevel()
window.move_in_from_bottom(480) # That's some resolution (height)

When I run this, I get the window correctly displayed at final position (0,0) and all coordinates are printed out from (0,479) down to (0,0). But there are no windows displayed in between, altough I am calling deiconify().
Can anybody help me out? I am confused. ^^

Thank's in advance. Greetings!

Upvotes: 1

Views: 137

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386220

Look at this line of code:

self.after(5, self.move_in_from_bottom(y))

In that line of code you are immediately calling self.move_in_from_bottom before the call to after, and the result of that call (None) is being passed to the after command.

The after method needs a reference to a function. A common way to do that is to use lambda, though functools.partial works well, too.

Here is an example using lambda:

self.after(5, lambda y=y: self.move_in_from_bottom(y))

Upvotes: 1

Related Questions