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;

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

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/

 

转载于:https://www.cnblogs.com/GarfieldTom/archive/2013/01/14/2860206.html

PythonForDelphi 修改 最后修改日期2019-2-1这一版,需要注意的是python 3.7 要安装32位版 需要在Delphi中先安装上PythonForDelphi控件包,安装不麻烦,可参考上述资料的说明文档. 包含34个例程源码几乎涵盖了Python4Delphi的所有方面. Demo01 A simple Python evaluator Demo02 Evaluate a Python expression Demo03 Defining Python/Delphi vars Demo04 Defining Python/Delphi vars (advanced case) Demo05 Defining a new Module Demo06 Defining a new Type Demo07 Using Delphi methods as Python functions Demo08 Using Delphi classes for new Python types Demo09 Making a Python module as a Dll Demo10 Mapping Delphi VCL components inside Python using TPythonDatabase (BDE - not changed) Demo10_FireDAC Database demo using FireDAC Demo11 Using Threads inside Python Demo12 Using PythonAtom. -> Deprecated since Delphi 6, See VarPyth instead Demo13 Using TPythonDatabase. (BDE - not changed) Demo14 Making a Dll with TPythonDatabase (BDE - not changed) Demo15 Using a TDataset descendant with Python, except TTable and TQuery. (BDE - not changed) Demo16 Using a TDelphiVar or Module methods ? (Kylix ready) Demo17 Using variant arrays of 2 dimensions (Kylix ready) Demo18 C++ Builder: using the Python Dll in a console application Demo19 C++ Builder: this is a replicate of the Delphi Demo05 Demo20 C++ Builder: this is a replicate of the Delphi Demo08 Demo21 Using Events in TPythonModule or TPythonType (Kylix ready) Demo22 Using Threading, Windows Console and Command line arguments (Kylix ready) Demo23 Using Threading and Delphi log window. (Kylix ready) Demo24 Using TAtomPythonEngine (Deprecated since Delphi 6, See VarPyth instead) Demo25 Using VarPyth.pas (Kylix ready) Demo26 Demo8 revisited to allow the new Python type to be subclassed Demo27 Container indexing Demo28 Iterator (Kylix ready) Demo29 Using Python Imaging Library (PAL) Demo30 Using Named Parameters (Kylix ready) Demo31 Using WrapDelphi to access Delphi Form attributes (Requires Delphi5 or later) Demo32 Demo08 revisited using WrapDelphi (Requires Delphi7 or later) Demo33 Using Threads inside Python Demo34 Dynamically creating, destroying and recreating PythonEngine. Uses PytonVersions
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值