Tkinter的全局绑定 [英] Tkinter Global Binding

查看:236
本文介绍了Tkinter的全局绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能所有控件绑定到一个命令,一个单行?这将是很好,如果我能在同一行,而不是单独做每个插件类型。

Is it possible to bind all widgets to one command, with a single line? It would be nice if I could type in one line as opposed to doing each widget individually.

推荐答案

您将使用 bind_all在根窗口方法。那么这将适用于所有小(除非你删除一些小部件bindtag全)。请注意,这些绑定去年火,所以你仍然可以覆盖应用程序范围内,如果你想在特定的部件结合。

You would use the bind_all method on the root window. This will then apply to all widgets (unless you remove the bindtag "all" from some widgets). Note that these bindings fire last, so you can still override the application-wide binding on specific widgets if you wish.

下面是一个人为的例子:

Here's a contrived example:

import Tkinter as tk

class App:
    def __init__(self):
        root = tk.Tk()
        root.bind_all("<1>", self.woot)
        label1 = tk.Label(text="Label 1", name="label1")
        label2 = tk.Label(text="Label 2", name="label2")
        entry1 = tk.Entry(name="entry1")
        entry2 = tk.Entry(name="entry2")
        label1.pack()
        label2.pack()
        entry1.pack()
        entry2.pack()
        root.mainloop()

    def woot(self, event):
        print "woot!", event.widget

app=App()

您可能也有兴趣在<一个href=\"http://stackoverflow.com/questions/3501849/how-to-bind-self-events-in-tkinter-text-widget-after-it-will-binded-by-text-widge/3513906#3513906\">my回答的问题<一href=\"http://stackoverflow.com/questions/3501849/how-to-bind-self-events-in-tkinter-text-widget-after-it-will-binded-by-text-widge\">How绑定在Tkinter的文本小部件自事件后,它将被文本控件绑定?,我谈一点关于bindtags。

You might also be interested in my answer to the question How to bind self events in Tkinter Text widget after it will binded by Text widget? where I talk a little more about bindtags.

这篇关于Tkinter的全局绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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