前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python For Delphi---

Python For Delphi---

作者头像
py3study
发布2020-01-10 18:12:13
2.6K0
发布2020-01-10 18:12:13
举报
文章被收录于专栏:python3python3

先上相关资源的下载吧:

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.

上代码:

代码语言:javascript
复制
unit FfrmMain;

interface

uses
  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代码:

代码语言:javascript
复制
Result.Value=int(Num1.Value)+int(Num2.Value)

在执行按钮中填加代码:

代码语言:javascript
复制
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,放到程序目录下):

代码语言:javascript
复制
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中写下面的代码:

代码语言:javascript
复制
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中将看到下面的内容:

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档