如何在tkinter中创建模式对话框? [英] How to create a modal dialog in tkinter?

查看:338
本文介绍了如何在tkinter中创建模式对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行一些嵌入式Python脚本的MFC应用程序.我正在尝试使该嵌入式脚本创建模态对话框之一,但是我没有取得太大的成功.

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.

有人可以指出我进行模态对话的方法吗?我需要为此使用Windows函数还是仅Tk或Python函数就足够了?

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)

推荐答案

grab_set是使窗口成为应用程序模式"的适当机制.也就是说,它从同一应用程序中所有其他窗口(即同一进程中的其他Tkinter窗口)获取所有输入,但是它允许您与其他应用程序进行交互.

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.

如果希望对话框为全局模式,请使用grab_set_global.这将接管整个系统的所有键盘和鼠标输入.使用此功能时必须格外小心,因为如果您有一个阻止应用释放释放错误的错误,就很容易将自己锁定在计算机之外.

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.

这篇关于如何在tkinter中创建模式对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆