栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python For Delphi---更好地协同

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python For Delphi---更好地协同

先上相关资源的下载吧:

python4delphi:

主页:

http://code.google.com/p/python4delphi/

下载:

svn checkout http://python4delphi.googlecode.com/svn/trunk/ python4delphi-read-only

现在已支持到XE2.

必看(作者):

http://www.atug.com/andypatterns/pythonDelphiTalk.htm

下面要示范的就是在XE2下完成.其实源码检出后,里面有30多个示例,几乎涵盖了Python4Delphi的所有方面.好吧,我们下面做个简单的加法计算器,主要是演示二者之间的参数传递.

当然,需要在Delphi中先安装上PythonForDelphi控件包,安装不麻烦,可参考上述资料的说明文档.

在XE2中新建一个工程,然后在窗口中依次放上一个TPythonEngine,三个TPythonDelphiVar,TPythonDelphiVar的VarName分别设置为Num1,Num2,Result.再放上三个LabelEdit,分别命名为edtNum1,edtNum2,edtResult.

上代码:

unit FfrmMain;interfaceuses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, RzPanel,  PythonEngine, PythonGUIInputOutput, RzButton;type  TfrmMain = class(TForm)    memInput: TMemo;    Splitter1: TSplitter;    memOutput: TMemo;    RzPanel1: TRzPanel;    pyEngine: TPythonEngine;    PythonGUIInputOutput1: TPythonGUIInputOutput;    btnExcute: TRzBitBtn;    PythonDelphiVar1: TPythonDelphiVar;    PythonDelphiVar2: TPythonDelphiVar;    edtNum1: TLabeledEdit;    edtNum2: TLabeledEdit;    edtResult: TLabeledEdit;    PythonDelphiVar3: TPythonDelphiVar;    procedure btnExcuteClick(Sender: TObject);    procedure PythonDelphiVar1GetData(Sender: TObject; var data: Variant);    procedure PythonDelphiVar2GetData(Sender: TObject; var data: Variant);    procedure PythonDelphiVar3SetData(Sender: TObject; data: Variant);  private    { Private declarations }  public    { Public declarations }  end;var  frmMain: TfrmMain;implementation{$R *.dfm}procedure TfrmMain.btnExcuteClick(Sender: TObject);begin  pyEngine.ExecStrings(memInput.Lines);end;procedure TfrmMain.PythonDelphiVar1GetData(Sender: TObject; var data: Variant);begin  data:=edtNum1.Text;end;procedure TfrmMain.PythonDelphiVar2GetData(Sender: TObject; var data: Variant);begin  data:=edtNum2.Text;end;procedure TfrmMain.PythonDelphiVar3SetData(Sender: TObject; data: Variant);begin  edtResult.Text:=Data;end;end.

上面窗体中还放了两个memo和一个TPythonGUIInputOutput,这些可以不用.

然后在memInput中输入Python代码:

Result.Value=int(Num1.Value)+int(Num2.Value)

在执行按钮中填加代码:

pyEngine.ExecStrings(memInput.Lines);

当然,可以直接执行上面的Python代码.

在edtNum1中输入一个数字,在edtNum2中输入一个数字,点击按钮,执行python脚本后就可以在edtResult中返回计算结果.

注意:

Result.Value=int(Num1.Value)+int(Num2.Value)

TPythonDelphiVar传人的是字符类型,所以要转换为int后再相加,否则是字符串相加.

这样,我们就完成了Delphi传递参数到Python,Python执行完毕后将结果再返回给Delphi的演示.好了,我们可以好好利用Python,将它很好地嵌入到Delphi中了.

如果要传递更复杂的参数怎么办?我想,或许可以将要传递的参数JSON化,然后将JSON作为参数在二者之间相互传递,这样可以完成更复杂的功能.

附上Python JSON文档:

http://docs.python.org/2/library/json.html

Delphi JSON之SuperObj:

http://www.progdigy.com/?page_id=6

http://code.google.com/p/superobject/

Delphi通过PythonForDelphi变量来和Python交换数据可以,有没有别的办法了呢?有,可以像COM一样来调用Python模块的变量和函数,这看起来好像能更酷一些 :-)

感谢samson,是他的一篇文章使我学习到了这个方法,并且很热心地给予了指教!

废话少说,先上Python代码(hello.py,放到程序目录下):

strPython='Hello,This is a python string !'dicPython={'StringInfo':'Hello,This is a python string !'}lstPython=list('Hello,This is a python string !')def SayHello(s):    return 'Hello,'+s

上面是简单的示例,有变量和函数,我们看看在Delphi中怎样来调用.
在Delphi中写下面的代码:

var  PyModule: variant;....  PyModule := import('hello');  //测试Python变量传递  Memo1.Lines.Add(PyModule.strPython);  Memo1.Lines.Add(PyModule.dicPython);  Memo1.Lines.Add(PyModule.lstPython);  Memo1.Lines.Add(PyModule.SayHello('Garfield'));

执行后,在Delphi的Memo1中将看到下面的内容:

Hello,This is a python string !{'StringInfo': 'Hello,This is a python string !'}['H', 'e', 'l', 'l', 'o', ',', 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'p', 'y', 't', 'h', 'o', 'n', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', '!']Hello,Garfield

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/223780.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号