简体   繁体   English

Python Tkinter-顶级MoveInAnimation

[英]Python Tkinter - Toplevel MoveInAnimation


Is there an option to simply move a Tkinter TopLevel() window on runtime by using an animation? 是否可以通过使用动画在运行时简单地移动Tkinter TopLevel()窗口? I thought about a smooth moveInAnimation triggered by a button. 我想到了由按钮触发的平稳的moveInAnimation。
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: 例如在mainFunction中这样调用:

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). 运行此命令时,我将窗口正确显示在最终位置(0,0),所有坐标都从(0,479)向下打印到(0,0)。 But there are no windows displayed in between, altough I am calling deiconify() . 但是之间没有显示任何窗口,尽管我正在调用deiconify()
Can anybody help me out? 有人可以帮我吗? I am confused. 我很困惑。 ^^ ^^

Thank's in advance. 提前致谢。 Greetings! 问候!

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. 在该行代码中,您立即self.move_in_from_bottom的调用之前调用self.move_in_from_bottom ,并且该调用的结果( None )被传递给after命令。

The after method needs a reference to a function. after方法需要引用一个函数。 A common way to do that is to use lambda, though functools.partial works well, too. 一种常见的实现方法是使用lambda,尽管functools.partial也可以很好地工作。

Here is an example using lambda: 这是使用lambda的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM