aardio 中调用 python 的方法有两种,py3 和 process.python 模块
py3 模块:如果经常要拿到python返回的值或从aardio中传数据给python去处理,aardio和python的交互比较多的话,可以考虑使用py3模块,缺点是:py3模块使用起来比较麻烦些,尤其是在异步多线程操作的话,中间需要学习的东西比较绕。
process.python 模块:如果aardio和python的交互不多(比如aardio负责做界面,python负责做爬虫或负责下载文件之类程序),只需后台由python负责完成相关工作的,用process.python来处理是非常方便的,而且使用logRespone可以完整回传Python中print或loggin打印出的信息给aardio的界面控件。 缺点:一般直接指定执行.py脚本,中间不好做交互,参数传递一般在execfile()命令执行时传递给python脚本,然后待脚本执行完成。
aardio 编辑GUI界面,调用 python 脚本示例。
编写 main.aardio 如下
import win.ui;
/*DSG{{*/
mainForm = win.form(text="读取PDF目录";right=899;bottom=511)
mainForm.add(
button1={cls="button";text="打开文件";left=627;top=21;right=702;bottom=43;z=2};
button2={cls="button";text="读取PDF目录";left=23;top=63;right=139;bottom=92;z=3};
button3={cls="button";text="读取目录.txt转换为FreeMind文件";left=153;top=62;right=352;bottom=91;z=4};
button4={cls="button";text="读取目录.txt文件转换为.json文件";left=365;top=63;right=563;bottom=91;z=5};
button5={cls="button";text="读取目录.txt转换为jstree模板所需的文件";left=581;top=64;right=815;bottom=91;z=6};
edit1={cls="edit";left=19;top=21;right=601;bottom=44;edge=1;z=1};
edit2={cls="edit";left=14;top=118;right=888;bottom=498;edge=1;multiline=1;readonly=1;vscroll=1;z=7}
)
/*}}*/import io;
import sys;
import fsys.dlg;//aardio调用python
import process.python;
import process.python.pip;
//process.python.pip("install pypdf2==2.12.1");
//process.python.pip("install xmltodict");
//process.python.pip("install jinja2");var f1, f2, tpath, py, txt;
// 打开文件
mainForm.button1.oncommand = function(id,event){mainForm.button1.disabled = true;f1 = fsys.dlg.open("pdf,文本文件|*.pdf;*.txt");if (io.exist(f1)){mainForm.edit1.text = f1;tpath = io.splitpath(f1);if (tpath.ext =='.txt'){txt = string.load(f1);mainForm.edit2.text = txt;}} else {mainForm.msgbox("file not found.");} mainForm.button1.disabled = false;
}// 读取PDF目录,生成PDF目录.txt
mainForm.button2.oncommand = function(id,event){mainForm.button2.disabled = true;f1 = mainForm.edit1.text;if (io.exist(f1)){tpath = io.splitpath(f1);if (tpath.ext =='.pdf'){py = process.python.execfile("\res\pdf_read_dir.py", f1);py.logResponse(mainForm.edit2)}}sleep(1000);f2 = string.replace(f1,'.pdf','.txt',1);if (io.exist(f2)){mainForm.edit1.text = f2;tpath = io.splitpath(f2);if (tpath.ext =='.txt'){txt = string.load(f2);mainForm.edit2.text = txt;}} else {mainForm.msgbox(f2++" not found.");}mainForm.button2.disabled = false;
}// 读取目录.txt转换为FreeMind(.mm)文件
mainForm.button3.oncommand = function(id,event){mainForm.button3.disabled = true;f1 = mainForm.edit1.text;if (io.exist(f1)){mainForm.edit1.text = f1;tpath = io.splitpath(f1);if (tpath.ext =='.txt'){py = process.python.execfile("\res\txt_xml_etree_mm.py", f1);py.logResponse(mainForm.edit2)}} else {mainForm.msgbox("file not found.");}mainForm.button3.disabled = false;
}// 读取目录.txt文件转换为.json文件
mainForm.button4.oncommand = function(id,event){mainForm.button4.disabled = true;f1 = mainForm.edit1.text;if (io.exist(f1)){tpath = io.splitpath(f1);if (tpath.ext =='.txt'){py = process.python.execfile("\res\txt_xml_etree_json.py", f1);py.logResponse(mainForm.edit2)}} else {mainForm.msgbox("file not found.");}f2 = string.replace(f1,'.txt','.json',1);mainForm.edit2.text = f2;mainForm.button4.disabled = false;
}// 读取目录.txt转换为jstree模板所需的html文件
mainForm.button5.oncommand = function(id,event){mainForm.button5.disabled = true;var f1 = mainForm.edit1.text;if (io.exist(f1)){tpath = io.splitpath(f1);if (tpath.ext =='.txt'){py = process.python.execfile("\res\txt_xml_etree_html.py", f1);py.logResponse(mainForm.edit2)}} else {mainForm.msgbox("file not found.");}f2 = string.replace(f1,'.txt','.htm',1);mainForm.edit2.text = f2;mainForm.button5.disabled = false;
}mainForm.show();
return win.loopMessage();
运行(F5)