問題描述
我有一個(gè) MFC 應(yīng)用程序,它運(yùn)行一些嵌入式 Python 腳本.我正在嘗試制作這個(gè)嵌入式腳本創(chuàng)建的對話框之一,但我沒有取得太大的成功.
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.
誰能指點(diǎn)我制作模態(tài)對話框的方法?我需要為此使用 Windows 函數(shù)還是僅使用 Tk 或 Python 函數(shù)就足夠了?
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?
對于我在 google 上搜索的內(nèi)容,以下函數(shù)組合應(yīng)該可以發(fā)揮作用,但它們似乎并沒有像我預(yù)期的那樣工作:
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)
推薦答案
grab_set
是使窗口應(yīng)用程序模式"的正確機(jī)制.也就是說,它接受來自同一應(yīng)用程序中所有其他窗口的所有輸入(即:同一進(jìn)程中的其他 Tkinter 窗口),但它允許您與其他應(yīng)用程序進(jìn)行交互.
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.
如果您希望對話框全局模態(tài),請使用 grab_set_global
.這將接管整個(gè)系統(tǒng)的所有鍵盤和鼠標(biāo)輸入.使用此功能時(shí)必須非常小心,因?yàn)槿绻龅阶柚箲?yīng)用釋放抓取的錯(cuò)誤,您可以輕松地將自己鎖定在計(jì)算機(jī)之外.
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.
當(dāng)我需要這樣做時(shí),在開發(fā)過程中,我會嘗試編寫一個(gè)防彈故障保險(xiǎn),例如一個(gè)在固定時(shí)間后釋放抓斗的計(jì)時(shí)器.
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.
這篇關(guān)于如何在 tkinter 中創(chuàng)建模態(tài)對話框?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!