Skip to content Skip to sidebar Skip to footer

Intercepting The Close Window Button (Tkinter Window) Throws An Tcl Error

I have a program that at some point opens a new window (filled with buttons and gizmo's for the user to select and play around with) that is defined as follows: def window(self,mas

Solution 1:

Let's examine this line of code:

top.protocol("WM_DELETE_WINDOW",close(self))

This line of code is saying "immediately call the function close(self), and assign the result to the protocol handler. See the problem? It's immediately calling close, likely before self has been fully constructed. You don't want the function to be called, you want to pass in a reference to the function.

Make close be a method of self (rather than an embedded function) and change the call to top.protocol to look like this (note the lack of trailing parenthesis):

top.protocol("WM_DELETE_WINDOW", self.close)

If you prefer to keep the nested function, you can use lambda:

top.protocol("WM_DELETE_WINDOW", lambda window=self: close(window))

Post a Comment for "Intercepting The Close Window Button (Tkinter Window) Throws An Tcl Error"