[过去半小时,我一直在阅读有关在Python中构造回调的各种方法,但当它“反弹”时,我还没有成功传递默认值以将其包括在内。
下面的代码演示了我希望通过退出Tkinter GUI发出回调时包括整数5。
class Heatmap(Updater):
""" Displays the Heatmap provided the data streaming in. """
def __init__(self, data_queue, slider_callback, closed_callback):
"""
Initialization function for the Osc heatmap.
:param data_queue: Data streamed in from the buffer for visualization.
:type data_queue: Queue
:param closed_callback: When the figure is closed, callback should be used to remove the figure.
:type closed_callback: Function
:param slider_callback: Callback function to return the state of the slider to parent caller.
:type slider_callback: Function
"""
super(Heatmap, self).__init__()
self.data_queue = data_queue
self.closed_callback = closed_callback
self.slider_callback = slider_callback
self.window = Tk()
#self.window.protocol('WM_DELETE_WINDOW', closed_callback)
atexit.register(self.closed_callback)
...
if __name__ == '__main__':
q = Queue()
for i in xrange(100):
q.put(i)
def close_cb(idx):
print 'idx {} window has been closed'.format(idx)
def slider_cb(val):
print 'slidy {}'.format(val)
closely = lambda x: close_cb(5)
hm = Heatmap(q, slider_cb, closely)
hm.run()
ERROR
TypeError: <lambda>() takes exactly 1 argument (0 given)
我看到错误指向我的atexit.register(self.closed_callback),因为它可能希望传递一些参数?在不传递故障信息的情况下如何满足这一要求,同时始终保持这样的事实,即我在成功的退出回调中收到5。
我也一直在玩functools.partial。
在过去的半小时中,我一直在阅读有关在Python中构造回调的各种方法,但当它“反弹”时,我还没有成功传递默认值。代码...
lambda
表达式不需要任何形式参数。
您在问题中提到您尝试过partial
,但没有向我们展示您如何尝试过。这是在您的情况下如何使用partial
的示例: