Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Variable in bind in tkinter python module

I was wondering if there is any way to get around using global variables in the callback functions that are used in bind in tkinter.

What I refer to is:

canvas = Canvas(root, width=500, height=500)
canvas.bind('<B1-Motion>', func)

where func is now some function that is triggered when the mouse is dragged. What I want is something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

canvas.bind('<B1-Motion>', func(arg))

In combination with:

def func(event, arg):
    commands

I can see from https://docs.python.org/3/library/tkinter.html that one argument, which is the event itself, is given to the callback function, but it seems like waste of potential to not give this method any way to modify its callback in a different way.

Maybe I am mistaken and there is some technical reason why that is impossible in general or maybe there is an alternative to bind.

I was basically expecting something like:

buttoname = Button(...,...,..., command =  Lambda: func(arg))

If anyone has any pointers, it would be much appreciated.

regards

>Solution :

Use a lambda that receives the event parameter and passes it along.

canvas.bind('<B1-Motion>', lambda e: func(e, arg))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading