[Tkinter-discuss] Re: making tkFileDialog and tkMessageBox modal on Win32

Andrew Zane azane at nnu.edu
Tue Dec 30 09:01:17 CET 2014


I had to add a little bit to make the dialog modal to the parent. So anyway, here’s to helping! : )
My comments are prefaced with an #A:, and my modifications are surrounded by the #A tag, #A - #/A, that is.

def tk_wrapgrab(tk, f, kw):
    #retrieved from: http://grokbase.com/t/python/tkinter-discuss/05468hr5qs/making-tkfiledialog-and-tkmessagebox-modal-on-win32
        # Wrap a dialog in a grab pair.
        # Use to make tkFileDialog and tkMessageBox application-modal.
        # Not needed for tkSimpleDialog.
        # tk - grab target, this is what will wait for the tkDialog to close
        # f - tkFileDialog.askopenfilename, tkMessageBox.showinfo, etc.
        # kw - dictionary of key-word arguments to f
    kw['parent'] = tk #A: No need to specify parent then. Grab target here is synonomous to parent.
    tk.grab_set() #A: Grab parent, making parent the only active window (of application) while tkDialog is open.
    retval = f(**kw)
    #A
    tk.wait_variable(retval) #A: Parent wait for the variable, disabling parent until tkDialog returns. Thus, tkDialogs are application-modal.
                             #      If we did a global grab, the tkDialog would be globally-modal.
    #/A
    tk.grab_release()
    return retval


More information about the Tkinter-discuss mailing list