[英]In Tkinter how do i remove focus from a widget?
我想手动从小部件中移除焦点。
You can focus to another dummy widget.您可以专注于另一个虚拟小部件。
Edit编辑
from Tkinter import *
def callback():
print master.focus()
master = Tk()
e = Entry(master)
e.pack()
e.focus()
b = Button(master, text="get", width=10, command=callback)
b.pack()
master.mainloop()
Focusing on a non-'focusable' widget will remove focus from another widget.专注于非“可聚焦”的小部件将移除另一个小部件的焦点。
w.focus_set()
and w.focus_force()
.w.focus_set()
和w.focus_force()
。 However, method w.focus_force()
is impolite.w.focus_force()
是不礼貌的。 It's better to wait for the window manager to give you the focus.takefocus
option.takefocus
选项。 Set takefocus to 0 to take your widget out of focus traversal (when user hits <Tab>
key).<Tab>
键时)。If the dummy widget is Canvas
then c.focus()
will not work.如果虚拟小部件是
Canvas
则c.focus()
将不起作用。
use c.focus_set()
or c.tk.call('focus',c)
to first focus on the canvas window itself.使用
c.focus_set()
或c.tk.call('focus',c)
首先关注画布窗口本身。
That's because那是因为
c.focus()
... returns the id for the item that currently has the focus, or an empty string if no item has the focus.
... 返回当前具有焦点的项目的 id,如果没有项目具有焦点,则返回空字符串。 Reference
参考
c.focus(id_)
will focus on the item having id id_
within the canvas. c.focus(id_)
将集中于具有项目ID id_
画布内。
c.focus("")
will remove the focus from any item in the canvas. c.focus("")
将从画布中的任何项目移除焦点。
Hence (within some callback)因此(在一些回调中)
c.config(highlightthickness = 0) # to remove the highlight border on focus
c.foucs_set()
c.focus("") # just to be sure
The reason c.focus()
functions differently is that within Tcl/Tk's Commands there's the "Primary" Command focus
c.focus()
函数不同的原因是在 Tcl/Tk 的命令中有“主要”命令focus
as well as the Canvas-specific Widget Command focus
以及 Canvas 特定的小部件命令
focus
That's not an issue within the Tcl/Tk syntax but in the tkinter module c.focus()
will call the underlying canvas-specific foucs
.这在 Tcl/Tk 语法中不是问题,但在
c.focus()
模块中c.focus()
将调用底层画布特定的foucs
。
From tkinter.py
within the Canvas
class Line 2549来自
Canvas
类 Line 2549 中的tkinter.py
def focus(self, *args):
"""Set focus to the first item specified in ARGS."""
return self.tk.call((self._w, 'focus') + args)
我的解决方案是root.focus()
它将删除小部件焦点。
If you use ttk widgets you can "remove" the focus ring by removing the color;如果您使用 ttk 小部件,您可以通过移除颜色来“移除”聚焦环; for example on a button:
例如在按钮上:
style = ttk.Style()
style.configure('TButton', focuscolor='')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.