在 Android Studio 中使用 Chaquopy 在 python 文件中插入函数值

Insert value to function in python file using Chaquopy in Android Studio

我正在尝试将 'Hello World' 插入到 python 文件中的 def m(input)。但是它不起作用我该如何解决?

main.py

def m(input):
    return input

MainActivity.java

if(!Python.isStarted())
    Python.start(new AndroidPlatform(this));

Python py = Python.getInstance();
PyObject pyf = py.getModule("main");
PyObject obj = pyf.callAttr("m('Hello World')");
text.setText(obj.toString());

这是我得到的错误

com.chaquo.python.PyException: AttributeError: module 'main' has no attribute 'm('Hello World')'

callAttr 的参数应单独传递,而不是作为单个 Python 表达式传递。例如:

pyf.callAttr("m", "Hello world");

有关详细信息,请参阅 callAttr documentation and the examples