【问题标题】:Insert value to function in python file using Chaquopy in Android Studio在 Android Studio 中使用 Chaquopy 将值插入到 python 文件中
【发布时间】:2021-06-11 17:29:59
【问题描述】:

我正在尝试将“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')'

【问题讨论】:

    标签: python android-studio chaquopy


    【解决方案1】:

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

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

    有关详细信息,请参阅 callAttr documentationexamples

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      相关资源
      最近更新 更多