Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python / tkinter: Events: call a function by mouse on widget

I would like to improve my program, I just do not know how.

I want that an event starts, when the users mouse is over a tkinter's widget.

This...

button.bind("<Motion>", lambda eff: callback())

... is working, but the users needs to move the mouse for call the function.

Is there a way to start, for example, every x seconds the function while the mouse is over the widget?

I need it to display an integer that changes constantly as text of a button or a label while the mouse is over the button or label.

Do you have any Ideas?

Thanks for your attention,

Lukas

like image 574
Lukas Avatar asked Jul 13 '26 04:07

Lukas


1 Answers

You can bind a function to <Enter> to trigger a function when the mouse enters a widget, and you can bind to <Leave> to trigger when it leaves.

The <Enter> binding can run a function that starts updating the label periodically using after. The <Leave> binding can set a flag that the update function checks in order to know to stop updating.

There are many questions and answers on this site about using after to do something periodically. For example, How to use the after method to make a callback run periodically?

like image 173
Bryan Oakley Avatar answered Jul 15 '26 22:07

Bryan Oakley