如何让窗口专注于 Tkinter 中的新 Toplevel() 窗口? [英] How to keep the window focus on new Toplevel() window in Tkinter?

查看:53
本文介绍了如何让窗口专注于 Tkinter 中的新 Toplevel() 窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个 win32gui python27 应用程序(我正在运行 win7).我想知道是否可以从我的 mainloop() 创建一个新窗口并保持对它的关注,可能是通过停止 mainloop 并停用根窗口.在一种伪代码示例中:

I am currently writing a win32gui python27 application (I'm running win7). I am wondering if it is possible to create a new window from my mainloop() and KEEPING the focus on it, possibly by stopping the mainloop and deactivating the root window. In a sort of pseudo code example:

root=Tk()
#put in some widgets, such as statusbars, tkfileDialog widgets etc...
O=Toplevel()
OptionMenu(O) #wait for user to make his choices; btw: OptionMenu is a class...
tkFileDialog.askdirectory(...) #THEN interpret this line

基本上,我想实现 tkfiledialog 和 tksimpledialog 中大多数小部件的功能:
从主窗口中窃取焦点(不仅仅是控制台焦点,wm 焦点)并恢复主循环,直到在 OptionMenu 中的所有内容都得到解决之前.我希望我能让大家清楚我的目标,我几周前才开始 Tkinter 编程,可能会混淆和误解它背后的一些概念......
就是这样,伙计们!

Basically, I'd like to achieve what most of the widgets in tkfiledialog and tksimpledialog do:
To steal the focus from the main window (not just the console focus, the wm focus) and to resume the mainloop until AFTER everything in, for example, OptionMenu has been resolved. I hope I could make my goals clear to y'all, I just started Tkinter programming a couple of weeks ago and may confuse and misinterpret some concepts behind it....
That's all, folks!

推荐答案

您正在寻找的概念称为抓取".Tkinter 支持使用多种方法进行抓取.例如,要在顶层设置本地抓取,您可以执行 my_window.grab_set().本地抓取是此窗口从您的应用程序中的所有其他窗口抓取焦点,但仅限于您的应用程序.

The concept you are looking for is called a "grab". Tkinter supports grabs with several methods. For example, to set a local grab on a toplevel you would do my_window.grab_set(). A local grab is where this window grabs the focus from all other windows in your app, but only for your app.

您还可以进行全局抓取,从而有效地冻结整个显示画面,但特定窗口除外.这是非常危险的,因为如果您的代码中有错误,您可以轻松地将自己锁定在自己的计算机之外.

You can also do a global grab, which effectively freezes your entire display except for your specific window. This is very dangerous since you can easily lock yourself out of your own computer if you have a bug in your code.

Grabs 不会停用主循环功能.这必须为您的窗口运行才能处理事件.它只是将所有事件重定向到具有抓取功能的窗口.

Grabs do not deactivate the mainloop function. This must be running for your window to process events. It simply redirects all events to the window that has the grab.

有关更多信息,请在此处阅读有关grab_set 和其他抓取命令的信息:http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.grab_set-method

For more information, read about grab_set and other grab commands here: http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.grab_set-method

这篇关于如何让窗口专注于 Tkinter 中的新 Toplevel() 窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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