0

I have an external script that calls the drawWorld() function of this class. I want the drawing to be shown for 1-2 seconds and then to close and the control to return to the main script. I can manage to let the window disappear with the line

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

but I cannot return the flow to the main script. I tried

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

but it doesn't work.

This is my code for the Tkinter class:

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() 
Alvin
  • 383
  • 5
  • 16
  • What does "I cannot return the flow to the main script" and "but it doesn't work." mean? Does it crash? Does the program hang? Do you get error messages? Can you provide a minimal script that illustrates the complete problem -- procedural code that calls this function and then attempts to continue when the function exits? – Bryan Oakley Oct 25 '13 at 14:25
  • Main script calls the drawWorld() function, therefore the control flow shifts to this portion of the code. I used the root.destroy() to remove the window and it works, while the root.quit() that is supposed to quit the tk environment (and let the rest of the script run) doesn't. I am sorry for not specifying but I meant not working = not quitting. Basically it stays open until I quit it manually (Ctrl-C). My external code is a simple: `import tkWorld tkWorld.drawWorld(); print "end"` I cannot get to the print line. I want the tk window to pop up and to die after 1s. – Alvin Oct 25 '13 at 14:42
  • your indentation is incorrect in your example. – Bryan Oakley Oct 25 '13 at 15:57

1 Answers1

0

In a comment to your question you wrote that your main program is just this:

import tkWorld
tkWorld.drawWorld()
print "end"

When I use that in a program, and use your example code (after fixing the indentation), it works fine. I see the window appear for one second, it goes away, and I send "end" printed on the console.

It works no matter whether the lambda calls root.quit() or 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"
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thank you Bryan, I tried it from a different machine and it works. I am a bit concerned now on what can be the problem on mine. Thank you anyways!! – Alvin Oct 25 '13 at 16:19