How to create a self-destroying child window to monitor an activity?
Question: As a followup to [this
question](http://stackoverflow.com/questions/17117156/create-a-python-tkinter-
window-with-no-x-close-button), I wanted to know whether there's a way in
Python/Tkinter to create a child window, have it show text progress updates
for a 30-60 second process, and then destroy itself when the process
completes. The process in question involves multiple steps such as file
copying, database updates, etc.

The linked-to question has a code sample showing the approach I used to create
a custom dialog window that collects user input and then closes after an
OK/Cancel button is clicked. I've found that using this same approach doesn't
work for the status-update child I want to create. If I put all the code
inside the `showWin` method, I hit a race condition where the window tries to
destroy itself before it's displayed (even when I used `update_idletasks`).
But if I try moving the file/DB code to a separate method, there's no event to
trigger it and so the processing never happens.

Do I just need to give up on using a child window for this purpose? Or is
there some arrangement that lets the processing happen while avoiding the race
condition?


Answer: I don't quite understand what you're trying to accomplish, but based solely on
the title of the question I'm guessing you want some sort of pop-up progress
window that destroys itself when progress is complete.

Here's a contrived example that displays a window that self-destructs when
progress passes 100%. It's not very bullet-proof, but it shows that it's
possible to create a window that self-destructs.

    
    
    import Tkinter as tk
    
    class App(tk.Frame):
        def __init__(self, *args, **kwargs):
            tk.Frame.__init__(self, *args, **kwargs)
            go_button = tk.Button(text="Go!", command=self.go)
            go_button.pack()
    
        def go(self):
            self.dialog = ProgressWindow(self)
            self.do_real_work(0)
    
        def do_real_work(self, count):
            # simulate doing work; update the status window periodically
            self.dialog.set(count)
            self.after(500, lambda count=count+10: self.do_real_work(count))
    
    
    class ProgressWindow(tk.Toplevel):
        def __init__(self, *args, **kwargs):
            tk.Toplevel.__init__(self, *args, **kwargs)
            self.label = tk.Label(self, text="0%")
            self.label.pack(side="top", fill="both", expand=True)
            self.wm_geometry("200x200")
    
        def set(self, value):
            if value > 100:
                self.destroy()
            else:
                self.label.configure(text="%s %%" % value)
    
    
    if __name__ == "__main__":
        root = tk.Tk()
        App(root).pack(side="top", fill="both", expand=True)
        root.mainloop()
    

