I have a MFC application which runs some embedded Python scripts. I am trying to make one of the dialogs this embedded script creates modal, but I am not having much success.
Can anyone point me the way to make a modal dialog? Do I need to use a windows functions for this or only Tk or Python functions are enough?
For what I googled looks like the following combination of functions should do the magic, but they dont seem to work the way I was expecting:
focus_set()
grab_set()
transient(parent)
In one of my projects I used the Tcl window manager attribute '-disabled' onto the parent window, that called a (modal) toplevel dialog window.
Don't know which windows you show with your MFC application are created or used with Tcl stuff, but if your parent window is Tk based you could do this:
In Python simply call onto the parent window inside the creation method of your toplevel window:
After you got what you want with your modal window don't forget to use a callback function inside your modal window, to enable inputs on your parent window again! (otherwise you won't be able to interact with your parent window again!):
A Tkinter (Tcl Version 8.6) Python example (tested on Windows 10 64bit):
For more information about Tcl window manager attributes, just take a look at the Tcl documentation: https://wiki.tcl.tk/9457
grab_set
is the proper mechanism for making a window "application modal". That is, it takes all input from all other windows in the same application (ie: other Tkinter windows in the same process), but it allows you to interact with other applications.If you want your dialog to be globally modal, use
grab_set_global
. This will take over all keyboard and mouse input for the entire system. You must be extremely careful when using this because you can easily lock yourself out of your computer if you have have a bug that prevents your app from releasing the grab.When I have the need to do this, during development I'll try to write a bulletproof failsafe such as a timer that will release the grab after a fixed amount of time.