TKinter Destroy Question

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Nov 20 04:57:08 EST 2003


On Thu, 2003-11-20 at 04:59, Rob wrote:
> My first GUI so be gentle...
> 
> When I start my program I call a class that runs the initial window. While
> in this class if a certain button is pressed it calls a function outside the
> class. This function then initially calls another function to
> "root.destroy()". Basically I want the current window gone so the function I
> just called can open it's own window. The problem I'm stuck with is that
> once this function is done and I need to close the new window to go to the
> next window i again call the function which performs the "root.destroy()".
> When I try to call it a second time it tells me:
> 
> TclError: can't invoke "destroy" command:  application has been destroyed
> 


When you destroy a window it's gone! so can't be destroyed again.....
Without an example of what you are doing it's difficult to help but.
You could call withdraw() method on root this will unmap it from the
screen (but not destroy it)  However why are you calling destroy on root
twice?  Perhaps you mean to call destroy on the old window when you
create a new one (like a Wizard)


e.g (untested...)

root=Tk()

def createNext():
    nextWindow = Toplevel()
    nextWindow.title("Next Window")
    b = Button(root, text="Create Next Window", command=createNext)
    b.pack()
    root.withdraw()    


root.title("The Main Root Window")
b = Button(root, text="Create Next Window", command=createNext)
b.pack()
root.mainloop()


The problem here is you are not getting rid of the previous window when
the Next button is pressed just root!


Perhaps a better example would be:-


class MyApplication(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.title("Main Application Window")
        self.prevWindow = self
        self.windowCount = 1
	b = Button(self, text="Next", command=self.nextWindow)
        b.pack()
    
    def nextWindow(self):
        window = Toplevel()
        self.windowCount = self.windowCount + 1
        window.title("Next Window %d" %self.windowCount)
	b = Button(window, text="Next", command=self.nextWindow)
        b.pack()
        self.prevWindow.withdraw()
        self.prevWindow = window




app = MyApplication()
app.mainloop()


However this will never finish!  to close the whole application the 
app.quit() method must be called..... or if it's inside the class
definition self.quit()

Basically explain what you want with some example code and we can help 

Regards
Martin


-- 
Martin Franklin <mfranklin1 at gatwick.westerngeco.slb.com>






More information about the Python-list mailing list