-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
  • 370,779
  • 53
  • 539
  • 685

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
  • 462,703
  • 120
  • 1,088
  • 1,195
-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
  • 16,341
  • 7
  • 39
  • 70