Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom events in Tkinter

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?

like image 525
namco Avatar asked Oct 24 '25 04:10

namco


1 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.

like image 114
Bryan Oakley Avatar answered Oct 25 '25 22:10

Bryan Oakley