详细看demo25的代码

These techniques are demonstrated in Demo25 in the examples folder of your Python for Delphi distribution.

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 info
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.

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.

var
   s : 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);
constLIB = 'E:\\ZopeWebSite\\bin\\lib';LIBDLL = 'E:\\ZopeWebSite\\bin\\DLLs';
varre : variant;m : variant;
beginmemo1.lines.Add('SysModule.path is ' + SysModule.path);memo1.lines.Add('');Assert(VarIsPythonSequence(SysModule.path));displaySysPath(ListBox1);    
  if not Boolean(SysModule.path.Contains(LIB)) thenSysModule.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 1
procedure TForm1.fixSysPath;
constLIB = '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);
varcurrdir, libdir : string;
begincurrdir := 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);
vars, lzt : variant;i : integer;
begins := VarPythonCreate(str);   // convert normal string into a python string.lzt := s.split(',');    
  for i := 0 to lzt.Length-1 dolstbox.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);
varlzt : variant;i : integer;
beginAssert(VarIsPythonSequence(SysModule.path));lzt := SysModule.path;for i := 0 to lzt.Length-1 dolstbox.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);
varm, doc, top : variant;s : string;
beginfixSysPath;displaySysPath(ListBox1);    
  m := Import('base64');showmessage(m);s := m.encodestring();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.

varmymodule, obj: variant;
begin    
mymodule := Import('LoginMgr');
obj := mymodule.LoginMgr();
obj.somemethod()    // call the method    

python4delphi 设置syspath相关推荐

  1. python delphi通信_python4delphi 设置syspath

    详细看demo25的代码 These techniques are demonstrated in Demo25 in the examples folder of your Python for D ...

  2. python4delphi和tesserocr库安装配置

    1.下载python4delphi控件包 https://github.com/pyscripter/python4delphi 2.在DelphiIDE里打开source下面对应pkg,然后comp ...

  3. delphi11中使用python4delphi组件

    1.下载. 2.编译. 打开: C:\Users\winfred\Documents\Embarcadero\Studio\Projects\python4delphi\Packages\Delphi ...

  4. Kubernetes 中 设置pod不部署在同一台节点上

    在k8s中,节点的调度主要由亲和性和污点来进行控制的.   而在亲和性部分由分为了节点亲和性和节点反亲和性.   节点亲和性是指在pod部署时,尽量(软策略)或者必须满足(硬策略)部署在某些节点上. ...

  5. 在Dockerfile中设置G1垃圾回收器参数

    在Dockerfile中设置G1垃圾回收器参数 ENV JAVA_OPTS="\ -server \ -XX:SurvivorRatio=8 \ -XX:+DisableExplicitGC ...

  6. nginx配置http、https访问,nginx指定ssl证书,阿里云腾讯云华为云设置nginx https安全访问

    nginx配置http.https访问 要设置https访问需要从对应的云厂商申请证书,并下载Nginx证书到服务器. 我这里从阿里云申请了免费的域名证书,然后将证书放置在服务器的/etc/ssl/. ...

  7. Not injecting HSTS header since it did not match the requestMatcher HSTS设置问题解决

    HSTS请求设置 错误描述:在使用文件上传功能时,form表单提交带有header数据的请求时遇到这个问题,报错如下: Not injecting HSTS header since it did n ...

  8. springboot设置文件上传大小(tomcat默认1M)

    application.yml # 设置文件上传大小(tomcat默认1M) server:tomcat:max-http-form-post-size: -1 spring:servlet:mult ...

  9. IDEA设置单个文件、单个包、单个项目的编码格式

    IDEA设置单个文件.单个包.单个项目的编码格式 File-> Settings-> File Enclodings 选择编码格式,确定即可. 注意:此处的编码格式设定以后,该包已经存在的 ...

最新文章

  1. 要想进入顶级数据公司,2020年数据科学10大技能帮你加分
  2. Linux系统上的文件类型
  3. pandas(六) -- 合并、连接、去重、替换
  4. Open*** 安装脚本
  5. Servlet全面讲解
  6. python扫题软件_python 实现端口扫描工具
  7. python基础编程语法-Python编程入门——基础语法详解(经典)
  8. 【POJ3461】Oulipo(字符串Hash)
  9. Palantir:野心贼大,想做世界的创新引擎(附纪要)| 国君计算机李沐华
  10. java api 第一个类是_java_8_第一个API
  11. mysql经典46_mysql练习46题 PDF 下载
  12. Modelsim 教程
  13. Python智能对话机器人实现
  14. SQL Server(express)安装教程
  15. 云宏武汉大学国际软件学院桌面云
  16. 水倒七分、茶倒八分、酒倒满
  17. python中文字符串比较模块_python比较字符串相似度,原创度检测工具
  18. Adobe国际认证设计师含金量,能否代表设计师的真实水平?
  19. 这可能是京东考前焦虑最重的一次618
  20. 想哪写哪_随笔20191203

热门文章

  1. stdio.h: C++输入输出操作
  2. 微信小程序开发--如何在swiper中显示两个item以及下一个item的部分内容
  3. 四川阆中上演“万人同品腊八粥”
  4. [转载]找回被误删的VISTA“显示桌面”图标
  5. docker 开启remote api
  6. Windows Server 2016虚拟机克隆后修改安全标识SID
  7. Cocos2d-x使用iOS游戏内付费IAP(C++篇)
  8. 学习CSS的背景图像属性background
  9. 引导程序为什么要org 07c00h
  10. JavaScript try/catch/finally 语句