2016-02-13 85 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'不是用來返回一個座標元組。 –