How to capture events on tkinter child widgets?

后端 未结 4 1830
谎友^
谎友^ 2021-02-15 13:51

In the following block, clicking on a_frame triggers the event handler on_frame_click, but clicking on a_label which is a child of a

相关标签:
4条回答
  • 2021-02-15 13:59

    Depending on what you're trying to do, you could bind everything

    print(a_label.bindtags())  # ('.!frame.!label', 'Label', '.', 'all')
    
    tk.bind_class('.', "<Button>", on_frame_click)
    
    0 讨论(0)
  • 2021-02-15 14:16

    Yes, you can do what you want, but it requires a bit of work. It's not that it's not supported, it's just that it's actually quite rare to need something like this so it's not the default behavior.

    TL;DR - research "tkinter bind tags"

    The Tkinter event model includes the notion of "bind tags". This is a list of tags associated with each widget. When an event is received on a widget, each bind tag is checked to see if it has a binding for the event. If so, the handler is called. If not, it continues on. If a handler returns "break", the chain is broken and no more tags are considered.

    By default, the bind tags for a widget are the widget itself, the widget class, the tag for the toplevel window the widget is in, and finally the special tag "all". However, you can put any tags you want in there, and you can change the order.

    The practical upshot of all this? You can add your own unique tag to every widget, then add a single binding to that tag that will be processed by all widgets. Here's an example, using your code as a starting point (I added a button widget, to show this isn't something special just for frames and labels):

    import Tkinter as tkinter
    
    def on_frame_click(e):
        print("frame clicked")
    
    def retag(tag, *args):
        '''Add the given tag as the first bindtag for every widget passed in'''
        for widget in args:
            widget.bindtags((tag,) + widget.bindtags())
    
    tk = tkinter.Tk()
    a_frame = tkinter.Frame(tk, bg="red", padx=20, pady=20)
    a_label = tkinter.Label(a_frame, text="A Label")
    a_button = tkinter.Button(a_frame, text="click me!")
    a_frame.pack()
    a_label.pack()
    a_button.pack()
    tk.protocol("WM_DELETE_WINDOW", tk.destroy)
    retag("special", a_frame, a_label, a_button)
    tk.bind_class("special", "<Button>", on_frame_click)
    tk.mainloop()
    

    For more on bindtags, you might be interested in my answer to the question How to bind self events in Tkinter Text widget after it will binded by Text widget?. The answer addresses a different question than the one here, but it shows another example of using bind tags to solve real world problems.

    0 讨论(0)
  • 2021-02-15 14:18

    I can't seem to find a direct method of automatically binding to child widgets (though there are methods of binding to an entire class of widgets and to all widgets in an application), but something like this would be easy enough.

    def bind_tree(widget, event, callback, add=''):
        "Binds an event to a widget and all its descendants."
    
        widget.bind(event, callback, add)
    
        for child in widget.children.values():
            bind_tree(child, event, callback, replace_callback)
    

    Just thought of this, but you could also put a transparent widget the size of a_frame on top of everything as a child of a_frame and bind the <Button> event to that, and then you could refer to a_frame as e.widget.master in the callback in order to make it reusable if necessary. That'd likely do what you want.

    0 讨论(0)
  • 2021-02-15 14:18

    Based on what it says in the Levels of Binding section of this online Tkinter reference, it sounds like it's possible because you can bind a handler to three different levels.
    To summarize:

    1. Instance Level: Bind an event to a specific widget.
    2. Class Level: Bind an event to all widgets of a specific class.
    3. Application Level: Widget independent -- certain events always invoke a specific handler.

    For the details please refer to the first link.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题