1

我正在尝试从 javascript 调用 brython 函数承诺它给出错误 ReferenceError: brythonListener is not defined如何解决这个问题?

python/brython 代码

<script type="text/python">
def execute(*args):
    print(str(args))
window.brythonListener=execute
</script>

javascript代码

            function(data){
                console.log(data) //till here code works
                brythonListener(data)
            }
        )

我在这里想念什么?

4

1 回答 1

4

问题的原因 ReferenceError: brythonListener is not definedbrythonListener在加载 brython 后创建的,以解决此问题,只需在 js 脚本调用 python 脚本时重新加载 brython

布莱顿脚本

<script type="text/python">
from browser import window

def execute(*args):
    print(str(args))

window.brythonListener = execute
</script>

将调用brython函数的Js脚本

<Script onload="brython()">
function(data){
  console.log(data)
  brythonListener(data)
})
</script>
于 2020-09-05T04:17:30.837 回答