简体   繁体   English

在 Tkinter 中,如何从小部件中移除焦点?

[英]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.专注于非“可聚焦”的小部件将移除另一个小部件的焦点。

  • Set focus to another widget to remove focus from the target widget is a good idea.将焦点设置到另一个小部件以从目标小部件中移除焦点是一个好主意。 There are two methods for this: 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.最好等待窗口管理器给你焦点。 Setting focus to parent widget or to the root window removes focus from the target widget.将焦点设置到父窗口小部件或根窗口会从目标窗口小部件中移除焦点。
  • Some widgets have takefocus option.一些小部件有takefocus选项。 Set takefocus to 0 to take your widget out of focus traversal (when user hits <Tab> key).将 takefocus 设置为 0 以使您的小部件脱离焦点遍历(当用户点击<Tab>键时)。

If the dummy widget is Canvas then c.focus() will not work.如果虚拟小部件是Canvasc.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='')

So the question may be a duplicate here , but the answer from @Bryan Oakley works perfectly for me in Python 3.8所以这个问题在这里可能是重复的,但@Bryan Oakley 的答案在 Python 3.8 中对我来说非常适合

root.focus_set()

Too easy...太容易了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM