如何使顶层以外的区域不可点击? [英] How can I make area outside toplevel unclickable?

查看:213
本文介绍了如何使顶层以外的区域不可点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在tkinter中创建一个顶层窗口,并且希望外部区域不可单击.因此,以这种方式,用户无法在顶级窗口被销毁后立即从顶层窗口中单击. (而且还会阻止能够从根Tk()创建相同的窗口)

I want to create a toplevel window in tkinter, and I want the outside area unclickable. So this way, the user cannot click out from the toplevel window, just after it's been destroyed. (And also preventing to be able to create the same window from the root Tk())

推荐答案

Tkinter支持一种称为抓取"的东西.当您在窗口上进行抓取时,所有事件都会通过小部件进行传递.即使您在窗口外单击,该单击也会在窗口中注册.

Tkinter supports something called a "grab". When you put a grab on a window, all events are funneled through the widget. Even if you click outside the window, the click will register with the window.

有两种类型的抢夺:本地抢夺和全球抢夺.本地意味着抓取仅适用于您的应用程序-例如,您仍然可以单击桌面.全局抓斗适用于整个计算机.这些操作非常危险,因为如果您不提供释放抓斗的方法,则可以完全锁定UI.

There are two types of grabs: local and global. Local means the grab only works for your application -- you can still click on the desktop, for example. A global grab works for the whole computer. These can be very dangerous because you can completely lock up your UI if you don't provide a way to release the grab.

要设置本地抓斗,您可以调用 grab_set 任何小部件,所有事件都将转到该小部件.要设置全局图,请调用 grab_set_global .

To set a local grab you can call grab_set on any widget, and all events will go to that widget. To set a global graph, call grab_set_global.

本地抓取是tkinter实现模态对话框的方式-对话框打开时,它具有本地抓取,因此您必须先关闭对话框,然后单击主窗口中的按钮.

A local grab is how tkinter implements modal dialogs -- while the dialog is open it has a local grab so that you must dismiss the dialog before clicking on buttons in the main window.

危险的威尔·罗宾逊!如果您正在使用全局抓斗,请确保有绝对安全的方法来释放抓斗.例如,在开发过程中,您可以使用after在15秒后释放抓斗.或者,绑定到转义键.始终要始终先进行本地抓取测试.根据经验,除非绝对必要,否则应避免使用全局抓斗.

Danger Will Robinson! if you are working with global grabs, make sure there's a absolutely foolproof way to release the grab. For example, during development you might use after to release the grab after 15 seconds. Or, bind to the escape key. Always, always test with a local grab first. As a rule of thumb, however, you should avoid using a global grab unless absolutely necessary.

这篇关于如何使顶层以外的区域不可点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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