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()
or on_paint event for canvas widget after drawing some graphics on canvas, etc?

- 6,208
- 20
- 59
- 83
-
Well, depending on your context: suppose you have a button widget you want to press on so that a cricle would be drawn: in that case, when you cread the button you can add the option `command` to it so that to call the function. `my_button = Tkinter.Button(text = 'Press me', command = draw_cricle_method)`. – Billal Begueradj Mar 26 '16 at 16:16
-
I am sorry, but I explained explicitly, that I want to catch events that are not i n default event list of Button, for example, How can I catch packed event? You told about click event, which is in event list of button widget. – namco Mar 26 '16 at 16:20
2 Answers
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.

- 370,779
- 53
- 539
- 685
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.

- 370,779
- 53
- 539
- 685

- 18,414
- 3
- 40
- 52