python4delphi 设置syspath
2015-08-27 17:00
926 查看
详细看demo25的代码 These techniques are demonstrated in Demo25 in the examples folder of your Python for Delphi distribution.
on the AndyDelphiPy functions.)
New way (requires Delphi 6) // Drop a TPythonAtomEngine onto your form
// or datamodule and name it PE
// You also need to drop a pythonDelphiVar
// component and call it pdv uses AndyDelphiPy; var
obj : olevariant;
begin AndyDelphiPy.PyExeFile('LoginMgr.py', PE);
obj := AndyDelphiPy.PyClass('LoginMgr()', pdv, PE);
obj.somemethod() // call the method // Drop a TPythonAtomEngine or TPythonEngine
// onto your form or datamodule. uses VarPyth; var
mymodule, obj: variant;
begin mymodule := Import('LoginMgr');
obj := mymodule.LoginMgr();
obj.somemethod() // call the method Note that it it possible to slightly mix the old and new way, so that if you use the AndyDelphiPy.PyExeFile('LoginMgr.py', PE); to import the module then you can then switch to the new way, declare an obj: variant; then instantiate an instance using obj := MainModule.LoginMgr(); However you still need Delphi 6 and so you might as well just use the new way properly.
s : widestring;
begin
s := mypythonclassinstance.somemethod() ;
showmessage(s) ;
This function also calls VarIsPythonSequence which tests to see if the parameter passed is a list or not.
Technique 2
NOTE: Simply putting a window path as the currdir will ultimately fail since the paths returned by Delphi have single slashes and python needs wither unix slashes or \\ slashes. See here for an algorithm to handle this.
NOTE: See the python for delphi deployment section for a more in-depth discussion of paths.
Supplimentary utility to display the python syspath in a delphi gui control.
procedure TForm1.btnDisplaySysPathClick(Sender: TObject);
begin
ListBox1.clear;
displaySysPath(ListBox1);
end;
The old vs. the new ways.
Because Delphi 6 has custom variants, they can point to specific smart proxies for python objects. Before Delphi 6, you could have an oleVariant pointing to a python instance, but you couldn't do the smart things like know that it was a list type etc. The following examples assume you have a module called LoginMgr.py in the python path or in the same folder as your application .exe that you have built in Delphi. Note that the python file LoginMgr.py contains within it a python class called LoginMgr which has a method called somemethod. Old way (see my basic tutorial for more infoon the AndyDelphiPy functions.)
New way (requires Delphi 6) // Drop a TPythonAtomEngine onto your form
// or datamodule and name it PE
// You also need to drop a pythonDelphiVar
// component and call it pdv uses AndyDelphiPy; var
obj : olevariant;
begin AndyDelphiPy.PyExeFile('LoginMgr.py', PE);
obj := AndyDelphiPy.PyClass('LoginMgr()', pdv, PE);
obj.somemethod() // call the method // Drop a TPythonAtomEngine or TPythonEngine
// onto your form or datamodule. uses VarPyth; var
mymodule, obj: variant;
begin mymodule := Import('LoginMgr');
obj := mymodule.LoginMgr();
obj.somemethod() // call the method Note that it it possible to slightly mix the old and new way, so that if you use the AndyDelphiPy.PyExeFile('LoginMgr.py', PE); to import the module then you can then switch to the new way, declare an obj: variant; then instantiate an instance using obj := MainModule.LoginMgr(); However you still need Delphi 6 and so you might as well just use the new way properly.
Widestrings
Declare your delphi strings widestrings if you want to get more than 255 chars back from calls to python methods that return strings. e.g. vars : widestring;
begin
s := mypythonclassinstance.somemethod() ;
showmessage(s) ;
Booleans
If your python method call returns 1 or 0 and this is supposed to be interpreted as a boolean, then cast it inside Delphi e.g.if Boolean( mypythonclassinstance.somemethod()) then ....
Accessing syspath directly
Here is a function that accesses a global variable called SysModule and access the syspath directly.This function also calls VarIsPythonSequence which tests to see if the parameter passed is a list or not.
procedure TForm1.Button2Click(Sender: TObject); const LIB = 'E:\\ZopeWebSite\\bin\\lib'; LIBDLL = 'E:\\ZopeWebSite\\bin\\DLLs'; var re : variant; m : variant; begin memo1.lines.Add('SysModule.path is ' + SysModule.path); memo1.lines.Add(''); Assert(VarIsPythonSequence(SysModule.path)); displaySysPath(ListBox1);
if not Boolean(SysModule.path.Contains(LIB)) then SysModule.path.insert(0,LIB); SysModule.path.append(LIBDLL); memo1.lines.Add('SysModule.path now is ' + \ SysModule.path + #13#13); displaySysPath(ListBox1);
fixSysPath;
re := Import('re'); showmessage(re);
m := Import('xml.dom.minidom'); showmessage(m);
end;
Playing with sys paths
This is an example of how to set the python system path as seen by delphi's instance of the python interpreter (as represented by the pythonEngine component). Note that it is imperative that you have \\ as the slashed in your path as otherwise things like \fred will actually be interpreted as \f (whatever that escaped character is) plus 'red'. Technique 1procedure TForm1.fixSysPath; const LIB = 'E:\\ZopeWebSite\bin\\lib'; LIBDLL = 'E:\\ZopeWebSite\\bin\\DLLs'; begin
// this is illegal // SysModule.path.Clear;
// this will work with latest python for delphi components OK. //SysModule.path := NewPythonList;
// this is a boring but effective solution as well. while SysModule.path.Length > 1 do SysModule.path.pop;
SysModule.path.append(LIBDLL);
SysModule.path.append(LIB);
end;
Technique 2
procedure TForm1.btnClearSyspathToJustLibClick(Sender: TObject); var currdir, libdir : string; begin currdir := ExtractFilePath( Application.ExeName );
NOTE: Simply putting a window path as the currdir will ultimately fail since the paths returned by Delphi have single slashes and python needs wither unix slashes or \\ slashes. See here for an algorithm to handle this.
libdir := EnsurePathHasDoubleSlashes(libdir);
libdir := currdir + 'Lib';
SysModule.path := NewPythonList; // Relies on Jan 2002 install of python for Delphi components
SysModule.path.append(currdir);
SysModule.path.append(libdir);
end;
NOTE: See the python for delphi deployment section for a more in-depth discussion of paths.
Supplimentary utility to display the python syspath in a delphi gui control.
procedure TForm1.btnDisplaySysPathClick(Sender: TObject);
begin
ListBox1.clear;
displaySysPath(ListBox1);
end;
Writing a Delphi function that uses a python function to do the hard work.
Here is an example of writing a delphi utility function that takes a string, and splits it up (delimited by comma) and puts the result into a delphi list box. We are using python split function to do the splitting - cool eh?procedure TForm1.splitAstring(str:string; lstbox: TListBox); var s, lzt : variant; i : integer; begin s := VarPythonCreate(str); // convert normal string into a python string. lzt := s.split(',');
for i := 0 to lzt.Length-1 do
lstbox.Items.Add(lzt.GetItem(i))
end;
Displaying the python syspath in a delphi listbox
Even though we have a pointer to a python list object (via a Delphi variant), we still have to call .GetItem(i) on a python list rather than the python syntax of lzt[i] - why?Because we are in Delphi and thus we cannot use python syntax.procedure TForm1.displaySysPath(lstbox: TListBox);
var
lzt : variant;
i : integer;
begin
Assert(VarIsPythonSequence(SysModule.path));
lzt := SysModule.path;
for i := 0 to lzt.Length-1 do
lstbox.Items.Add(lzt.GetItem(i));
lstbox.Items.Add('----------------------------------');
end;
Loading python base64 and minidom module and processing XML in Delphi
procedure TForm1.minidomLoadClick(Sender: TObject); var m, doc, top : variant; s : string; begin fixSysPath; displaySysPath(ListBox1);
m := Import('base64'); showmessage(m); s := m.encodestring('this is some text which I am going to encode then decode again.'); showmessage(s + #13+#13 + m.decodestring(s));
m := Import('xml.dom.minidom'); doc := m.Document(); showmessage(doc);
top := doc.createElement( 'Workspace' ); top.setAttribute('Version', '1.1 beta4'); doc.appendChild(top);
s := doc.toxml(); showmessage('doc.toxml()' + #13+#13 + s);
end;
Importing your own class
Ensure you have a TPythonAtomEngine or TPythonEngine onto your form or datamodule.var mymodule, obj: variant; begin
mymodule := Import('LoginMgr'); obj := mymodule.LoginMgr(); obj.somemethod() // call the method
相关文章推荐
- Delphi 指针大全
- Delphi 异常抛出与捕获
- Delphi 窗体程序接受命令行参数
- Delphi XE7中蓝牙 Bluetooth例子的使用
- 一句话改变TWinControl控件的left坐标的前世今生(入口函数是SetBounds,然后调用SetWindowPos起作用,并发消息更新Delphi的left属性值)
- Delphi控件的显示内容与显示边框是两回事
- python4delphi 安装
- 怎样在delphi中实现控件和窗体的拖拽
- Delphi日期时间 UNIX
- Delphi 调用dll中的窗体
- DELPHI XE5安装
- Delphi SendMessage PostMessage 原理和区别
- Delphi dll定义与调用
- EPASWMM界面部分编译
- github上的一些Delphi开源项目
- delphi创建具有托盘的服务程序(service)
- Delphi ADOQuery连接数据库的查询、插入、删除、修改
- Delphi 修改注册表使程序开机启动
- Delphi6 调试
- Delphi6 IDE 一些配置说明