Tkinter 全局绑定 [英] Tkinter Global Binding

查看:29
本文介绍了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 方法.然后这将应用于所有小部件(除非您从某些小部件中删除绑定标签all").请注意,这些绑定最后触发,因此您仍然可以根据需要覆盖特定小部件上的应用程序范围的绑定.

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.

这是一个人为的例子:

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()

您可能还对 我的回答 如何在 Tkinter Text 小部件被 Text 小部件绑定后绑定 self 事件? 我在这里多谈一点绑定标签.

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天全站免登陆