的问题是,你的代码永远不会允许窗口重绘自身。睡眠会导致程序停止,因此不会输入事件循环,并且这是导致窗口被绘制的事件循环。
不是睡觉,而是利用事件循环,每N毫秒更新一次属性,直到获得所需的alpha透明度。
下面是一个可在mac上使用的例子。我认为它也适用于Windows。
import Tkinter as tk
class App:
def __init__(self):
self.root = tk.Tk()
self.count = 0
b=tk.Button(text="create window", command=self.create_window)
b.pack()
self.root.mainloop()
def create_window(self):
self.count += 1
t=FadeToplevel(self.root)
t.wm_title("Window %s" % self.count)
t.fade_in()
class FadeToplevel(tk.Toplevel):
'''A toplevel widget with the ability to fade in'''
def __init__(self, *args, **kwargs):
tk.Toplevel.__init__(self, *args, **kwargs)
self.attributes("-alpha", 0.0)
def fade_in(self):
alpha = self.attributes("-alpha")
alpha = min(alpha + .01, 1.0)
self.attributes("-alpha", alpha)
if alpha < 1.0:
self.after(10, self.fade_in)
if __name__ == "__main__":
app=App()
谢谢,我不知道睡眠有这样做的倾向。 – rectangletangle 2010-09-04 21:25:54