python tkinter button响应函数返回值_Python – 从Tkinter回调返回

如何从作为Tkinter回调执行的函数中获取返回的对象?

import Tkinter as Tk

from functools import partial

def square(x):

return x*x

root = Tk.Tk()

var = Tk.IntVar(root, value=0) #the variable the gets passed to the class call

menu = Tk.OptionMenu(root, var, *[0,1,2,3,4,5]) #a drop-down list to choose a value for the variable

menu.pack()

button = Tk.Button(root, text=’click’, command = partial(square,var.get())) #a button that calls the class

button.pack()

root.mainloop()

显然这是一个简化的例子.实际上,按钮调用的函数将返回对象,我希望将这些对象附加到将在主Python命名空间中保存以进行进一步操作的对象列表.

无论如何,用户可以使用GUI为该功能选择参数,然后按下将执行该功能的按钮.然而,函数的返回值似乎注定要输给以太,因为回调不会接受返回.如果不在square(x)的定义中使用丑陋的全局,这可以克服吗?