What does the wait_window method do?

It seems that the object that calls this method waits while the window is passed as the parameter to be destroyed before continuing with its own loop ...

From the lines of a document of the Misc class, you can observe:

 def wait_window(self, window=None): """Wait until a WIDGET is destroyed. If no parameter is given self is used.""" 

At first glance it seems that this method can be done by the Toplevel mod, but it is not. To make the Toplevel mod, we must use the grab_set() method.

I see around other explanations:

wait_window does not seem to return until the given widget is passed as the parameter is not destroyed.

From another place:

wait_window(widget) - creates a local event waiting for this widget to be destroyed. This loop does not affect MainLoop.

From effbot documentation:

wait_window introduces a local event loop and does not return until this window is destroyed (either using the destroy method or explicitly through the window manager):

 widget.wait_window(window) 

What exactly means that window waiting for window (itself)?

It seems that the code that comes after the wait_window call is not executed until the window passed to the same method is destroyed. In the following working example, we can see evidence of what has just been said:

 from tkinter import * def on_win_request(parent): dialog = Toplevel() parent.wait_window(dialog) # executed only when "dialog" is destroyed print("Mini-event loop finished!") r = Tk() b = Button(r, text='New Window', command=lambda: on_win_request(r)) b.pack() b2 = Button(r, text='Hello!', command=lambda: print("hello")) b2.pack() r.mainloop() 

"Mini-event loop finished!" will be printed only when the local Toplevel widget named dialog destroyed.

So, in what particular circumstances should I use this method?

+7
python dialog tkinter
source share
1 answer

As with the documentation, it waits until this window is destroyed. It is mainly used for modal pop-ups, although it does not make the mod modal itself. A function call simply does not return until the target window is destroyed. To make a modal window, you also need to capture.

The most common use is to create an instance of Toplevel, populate this window with widgets, and then wait for the window to be fired before performing any other action. While it waits, tkinter can continue to process events as usual.

For example, you can disable (or delay the creation) of the main graphical user interface, open a notification of the terms of service and wait for the user to confirm the terms of service, copyrights, license, etc. After the window is destroyed, you can complete the initialization or enable some widgets, etc.

The standard file dialog box is a great example: you open a dialog box and then your code waits for the user to pick up the file, then it will use the returned file name. Inside the implementation of the dialog, wait_window used wait_window that it does not return until the dialog is rejected.

+8
source share

All Articles