你如何检查一个小部件是否集中在Tkinter? [英] How do you check if a widget has focus in Tkinter?

查看:149
本文介绍了你如何检查一个小部件是否集中在Tkinter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tkinter导入的$

  * 

app = Tk()
text_field =入口(app)
text_field.pack()
app.mainloop()

我希望能够检查 text_field 目前被选中或关注,所以当用户按下回车键时,我知道是否对其内容做了些什么。

解决方案

如果只有当焦点位于条目窗口小部件上时才按下回车键,则只需向条目窗口小部件中添加一个绑定即可。它只会在小部件有焦点的时候触发。例如:

 >>>来自Tkinter import * 
>>> root = Tk()
>>> e1 = Entry(root)
>>> e1.pack()
>>> e2 = Entry(root)
>>> e2.pack()
>>> def handleReturn(event):
... printreturn:event.widget is,event.widget
... printfocus is:,root.focus_get()
。 ..
>>> e1.bind(< Return>,handleReturn)

如果你真的想要一个全局绑定,并且需要知道哪个部件有焦点,那么在根目录下使用focus_get()方法目的。在下面的例子中,绑定放在。上。 (主顶层),所以无论焦点是什么都会触发:

 >>>来自Tkinter import * 
>>> root = Tk()
>>> e1 = Entry(root)
>>> e1.pack()
>>> e2 = Entry(root)
>>> e2.pack()
>>> def handleReturn(event):
... printreturn:event.widget is,event.widget
... printfocus is:,root.focus_get()
。 ..
>>> root.bind(< Return>,handleReturn)

注意两者之间的区别:in第一个例子,只有当您按下第一个输入窗口小部件中的return时才会调用处理程序。没有必要检查哪个小部件有焦点。在第二个例子中,处理程序将被调用,无论哪个小部件有焦点。



这两种解决方案都很好,取决于您真正需要发生的事情。如果您的主要目标是只在用户按下特定窗口小部件中的返回值时才执行某些操作,请使用前者。如果你想要一个全局绑定,但是在这个绑定中,根据有或没有焦点做一些不同的事情,可以使用后面的例子。


from Tkinter import *

app = Tk()
text_field = Entry(app)
text_field.pack()
app.mainloop()

I want to be able to check if text_field is currently selected or focused, so that I know whether or not to do something with its contents when the user presses enter.

解决方案

If you want to do something when the user presses enter only if the focus is on the entry widget, simply add a binding to the entry widget. It will only fire if that widget has focus. For example:

>>> from Tkinter import *
>>> root=Tk()
>>> e1=Entry(root)
>>> e1.pack()
>>> e2=Entry(root)
>>> e2.pack()
>>> def handleReturn(event):
...     print "return: event.widget is",event.widget
...     print "focus is:", root.focus_get()
... 
>>> e1.bind("<Return>", handleReturn)

Notice that the handler is only called if the first entry has focus when you press return.

If you really want a global binding and need to know which widget has focus, use the focus_get() method on the root object. In the following example a binding is put on "." (the main toplevel) so that it fires no matter what has focus:

>>> from Tkinter import *
>>> root=Tk()
>>> e1=Entry(root)
>>> e1.pack()
>>> e2=Entry(root)
>>> e2.pack()
>>> def handleReturn(event):
...     print "return: event.widget is",event.widget
...     print "focus is:",root.focus_get()
... 
>>> root.bind("<Return>",handleReturn)

Notice the difference between the two: in the first example the handler will only be called when you press return in the first entry widget. There is no need to check which widget has focus. In the second example the handler will be called no matter which widget has focus.

Both solutions are good depending on what you really need to have happen. If your main goal is to only do something when the user presses return in a specific widget, use the former. If you want a global binding, but in that binding do something different based on what has or doesn't have focus, do the latter example.

这篇关于你如何检查一个小部件是否集中在Tkinter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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