2016-02-13 91 views
-1

任何人都有一个想法,为什么下面的代码会产生错误?从func返回2个值

我叫FUNC获得鼠标坐标:

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

,然后我要分配的结果,新的变量在主:

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

通过这样做,我得到以下错误:

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

任何人都有一个想法,为什么发生这种情况?谢谢大家!

+0

你没有缩进? – putvande

+3

'app_root.bind'返回什么? – max

+0

你能提供完整的堆栈跟踪吗? –

回答

-2

这是不好的做法,但你可以声明变量的作用域为global在其他地方访问它们:

def button_click(event): 

    global x, y 

    x = event.x 
    y = event.y 
1

假设您使用的是Tkinterbind()只会将一个事件绑定到您的button_click回调并返回一个事件标识。从bind()文档字符串报价:

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

你不能指望bind()回你的button_click()事件处理程序返回。

+1

换句话说,'bind'不是用来返回一个坐标元组。 –