【问题标题】:What is the difference between focus and focus_set methods?focus 和 focus_set 方法有什么区别?
【发布时间】:2015-03-14 20:10:44
【问题描述】:

effbot.org 网站上,有一个部分在讨论Basic Widget Methods。其中两种方法是focusfocus_set

信不信由你,以下是我们在focus 方法下找到的参考:

焦点方法。

而谈论focus_set 方法的人说:

将键盘焦点移至此小部件。这意味着所有键盘 发送到应用程序的事件将被路由到此小部件。

我的问题是:这两种方法做同样的事情吗?如果没有,focus 是做什么的?

【问题讨论】:

    标签: python python-3.x tkinter focus


    【解决方案1】:

    是的,两者是一回事。准确地说,focusfocus_set 的别名。您可以从解释器中的快速测试中看到这一点:

    >>> import tkinter
    >>> tkinter.Text.focus is tkinter.Text.focus_set # Same function object
    True
    >>>
    >>> help(tkinter.Text.focus)
    Help on function focus_set in module tkinter:
    
    focus_set(self)
        Direct input focus to this widget.
    
        If the application currently does not have the focus
        this widget will get the focus if the application gets
        the focus through the window manager.
    
    >>>
    

    注意在focus 上调用help() 是如何调出focus_set 的文档的。

    【讨论】:

    • 好的。为什么 tkinter 方法有别名?这不是我第一次发现这样的情况......
    • 我认为只有 Tkinter 开发人员才能回答这个问题。 ;) Tkinter 库看起来总是有点臃肿。如果我不得不猜测,要么是 1) 像其他 GUI 工具包那样学习曲线更小,要么是 2) 更加方便(如果你能想到的话,Tkinter 就有)。
    • @Rinzler 我猜focus_set 是从 tcl 命令派生的,focus 是“更友好、更 Pythonic”的版本。我不知道哪个是最先添加的。我同意重复有点烦人,但弃用和删除并不容易。
    • @Rinzler - 每次安装 Python 时附带的 Python IDLE IDE 完全用 Python 和 Tkinter 编写。您可以在Lib/idlelib下的标准库中找到源代码。
    • @iCodez 是的,我知道,但是跟踪起来很复杂,文件很多......你认识其他人吗?
    猜你喜欢
    • 2010-12-13
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 2018-05-14
    • 2010-09-14
    • 2016-05-03
    • 2020-03-11
    相关资源
    最近更新 更多