How to capture events on tkinter child widgets?

后端 未结 4 1834
谎友^
谎友^ 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 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

提交回复
热议问题