Custom events in Tkinter

前端 未结 2 456
说谎
说谎 2021-01-15 15:00

I am new in Tkinter.
And I want to know is there any way to catch some custom events for widgets,
for example catch on_packed event after widget.pack()

相关标签:
2条回答
  • 2021-01-15 15:41

    The list of possible event types is far more extensive than the few you typically see used in example, such as Key, Button, Motion, and Mousewheel. Here is a partial list. It appears that packing should generate a Map event.

    0 讨论(0)
  • 2021-01-15 15:53

    The events you describe don't exist. You can use the event_generate method to create your own custom events if you wish. With that you could create your own widget classes that emit any custom events you want.

    Custom events must always be defined with double angle brackets. For example, the following line of code will create an event named <<OnPaint>>:

    the_canvas.event_generate("<<OnPaint>>")
    

    You can then bind to that event just like you do any other event:

    the_canvas.bind("<<OnPaint>>", do_on_paint)
    

    In the specific case of on_pack, there are events that probably do what you want at a more abstract level. For example, there are events that fire when a widget becomes visible (<Visibility>), changes size (<Configure>), and a few others.

    The official tcl/tk documentation lists supported events. See the bind man page.

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