-1

Anyone has an idea why the following code generates error??

I call a func to get mouse coordinates:

def button_click(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))
    return x, y

and then I want to assign the results to new variables in main:

x_cord, y_cord = app_root.bind('<ButtonRelease-1>', button_click)

by doing this I get the following error:

"x_cord, y_cord = app_root.bind('<ButtonRelease-1>', button_click)
ValueError: too many values to unpack"

Anyone has an idea why this is happening? Thank you everybody!

Bryan Oakley
  • 341,422
  • 46
  • 489
  • 636

2 Answers2

1

Assuming you are using Tkinter, bind() would only bind an event to your button_click callback and return an event identifier. Quote from the bind() docstring:

Bind will return an identifier to allow deletion of the bound function with unbind without memory leak.

You cannot expect bind() to return what your button_click() event handler returns.

alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
-2

It is bad practice but you can declare the scope of variables as global to access them elsewhere:

def button_click(event):

    global x, y

    x = event.x
    y = event.y
Malik Brahimi
  • 15,933
  • 5
  • 33
  • 65