'modal dialogs' with Tkinter

Eric Brunel eric_brunel at despammed.com
Tue Feb 22 09:30:10 EST 2005


On 22 Feb 2005 06:03:14 -0800, Sean McIlroy <sean_mcilroy at yahoo.com> wrote:

> I'd like to have a function f such that, when f is invoked, a Tk
> window w is presented in which a number of variables can be modified,
> and f returns the values that are indicated by the relevant
> menus/checkbuttons/etc at the time w gets closed. I've tried various
> ways of doing this, without success. Any assistance would be greatly
> appreciated.

Here are the necessary ritual incantations to create a modal dialog with Tkinter:

dlg = Toplevel()
# ... build the window ...
## Set the focus on dialog window (needed on Windows)
dlg.focus_set()
## Make sure events only go to our dialog
dlg.grab_set()
## Make sure dialog stays on top of its parent window (if needed)
dlg.transient(parentWdw)
## Display the window and wait for it to close
dlg.wait_window(dlg)

The callbacks for your "OK", "Cancel" or whatever buttons you want to use should then call dlg.destroy(). This will terminate the wait_window call and continue the execution of the code after it.

HTH
  - Eric Brunel -



More information about the Python-list mailing list