在pythonapi中“注册回调”是什么意思?

2024-09-28 19:25:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想深入了解Python3API是如何设计的,当使用api从提供python3.x解释器的应用程序内部提供的模块向UI添加元素时,我被要求注册一个回调以便能够使用我的函数/脚本:从Cpython的角度来看,“注册回调”意味着什么?在


Tags: 模块函数脚本api应用程序元素uicpython
2条回答

通常,这意味着您要创建一个函数:

def foo(some,arguments):
    pass #do something here

然后将该函数传递给提供的API中的某个类:

^{pr2}$

现在,API_class_instance将在文档环境下调用foo。在

Callback

In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback.

因此,如果您正在注册一个uiapi回调,那么您可能会将您创建的某个函数传递给另一个函数,该函数将负责在某些情况发生时调用您的函数。在

例如,您可能在UI中有一个按钮,并且您希望在单击该按钮时执行一些代码。您可以注册如下所示的回调:

def onclick():
    print 'Button clicked!'

# call the onclick() method when the 'click' event happens on the button
some_api.register_callback(button, 'click', onclick)

API代码可能如下所示:

^{pr2}$

相关问题 更多 >