使 Tkinter 小部件成为焦点 [英] Make Tkinter widget take focus

查看:42
本文介绍了使 Tkinter 小部件成为焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,它使用 Tkinter 弹出一个带有消息的窗口.我如何确保它获得焦点,以便用户不会错过它并明确地关闭窗口.代码是:

I have a script that uses Tkinter to pop up a window with a message. How do I make sure it takes focus so the user doesn't miss it and explicitly has to dismiss the window. the code is :

root = Tk()
to_read = "Stuff" 
w = Label(root, text=to_read)
w.pack()
root.mainloop()

推荐答案

您可以使用 focus_force 方法.请参阅以下内容:

You can use focus_force method. See the following:

但请注意文档:

w.focus_force()

w.focus_force()

强制输入焦点到小部件.这是不礼貌的.最好等待窗口管理器给你焦点.另请参阅下面的 .grab_set_global().

Force the input focus to the widget. This is impolite. It's better to wait for the window manager to give you the focus. See also .grab_set_global() below.

更新:它应该适用于 root.例如,尝试运行以下代码.它将创建一个窗口,您可以切换焦点.5 秒后,它会尝试抓住焦点.

Update: It should work on root. For example, try running the following code. It will create a window and you can switch focus. After 5 seconds, it will try to grab the focus.

from Tkinter import *

root = Tk()
root.after(5000, lambda: root.focus_force())
root.mainloop()

这篇关于使 Tkinter 小部件成为焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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