简体   繁体   中英

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM