Home > Mobile >  Variable in bind in tkinter python module
Variable in bind in tkinter python module

Time:12-29

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:

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

CodePudding user response:

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

canvas.bind('<B1-Motion>', lambda e: func(e, arg))
  • Related