0

我有一个调用此类的 drawWorld() 函数的外部脚本。我希望绘图显示 1-2 秒,然后关闭并且控件返回到主脚本。我可以设法让窗口随着线条消失

root.after(1000, lambda: root.destroy())

但我无法将流程返回到主脚本。我试过了

root.after(1000, lambda: root.quit())

但它不起作用。

这是我的 Tkinter 类代码:

from Tkinter import Tk, Canvas, Frame, BOTH

class World(Frame):

def __init__(self, parent):
    Frame.__init__(self, parent)   
    self.parent = parent              
    self.parent.title("CliffWorld")        
    self.pack(fill=BOTH, expand=1)

    canvas = Canvas(self)        

    canvas.create_rectangle(4, 4, 31, 31, 
        outline="#f11", fill="#1f1", width=1)
    canvas.pack(fill=BOTH, expand=1)


def drawWorld():
    root = Tk()
    ex = World(root)
    root.geometry("330x220+300+300")
    root.after(1000, lambda: root.destroy())
    root.after(1000, lambda: root.quit())
    root.mainloop() 
4

1 回答 1

0

在对您的问题的评论中,您写道您的主程序就是这样的:

import tkWorld
tkWorld.drawWorld()
print "end"

当我在程序中使用它并使用您的示例代码(修复缩进后)时,它工作正常。我看到窗口出现了一秒钟,然后消失了,然后我发送打印在控制台上的“结束”。

无论 lambda 调用root.quit()还是root.destroy().

from Tkinter import Tk, Canvas, Frame, BOTH

class World(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent              
        self.parent.title("CliffWorld")        
        self.pack(fill=BOTH, expand=1)

        canvas = Canvas(self)        

        canvas.create_rectangle(4, 4, 31, 31, 
            outline="#f11", fill="#1f1", width=1)
        canvas.pack(fill=BOTH, expand=1)


def drawWorld():
    root = Tk()
    ex = World(root)
    root.geometry("330x220+300+300")
    root.after(1000, lambda: root.destroy())
    root.after(1000, lambda: root.quit())
    root.mainloop() 

if __name__ == "__main__":
    import tkWorld
    tkWorld.drawWorld()
    print "end"
于 2013-10-25T16:00:53.917 回答