[Tutor] keep from opening multiple Toplevel windows

Kent Johnson kent37 at tds.net
Sat Oct 4 14:23:29 CEST 2008


On Fri, Oct 3, 2008 at 8:31 PM,  <dwbarne at earthlink.net> wrote:
> I have a Tkinter button widget that when pressed invokes a Toplevel window call each time. The Toplevel window thus generated has a close button on it. As you might guess, when multiple Toplevel windows are open, I can press on a 'close' button to '.destroy' the window, but all other Toplevel windows remain and do not respond to their 'close' buttons. I understand why THIS happens, but...
>
> The behavior I seek is that one and only one Toplevel window gets generated no matter how many times the original Tkinter button is pressed. A new Toplevel is generated only after the previous one is closed.

The way I would do this is to have the class containing the button
handler save a reference to the window. When the window is closed then
set the reference to None. Then the button handler can check to see if
it already has a window.

Alternately you could just create a single, hidden window at startup,
then the button shows the window and the close box just hides it
instead of destroying it.


More information about the Tutor mailing list