delphi打包python_Python for delphi教程

Related'Python for Delphi'Links on this site:

5ccb64891538ddb7048f717b2fe52cbd.gif

tutorial -Andy's Python Delphi Tutorial - Getting started with the basics.  Also use these techniques if you are using Delphi 5 or below, and Python 2.0 or below.   Includes info on getting these free components.

6813f0226d78c03e9f597268e6891b57.gif

code - Here are Andy's Extensions  - a delphi unit that adds a few utility functions to Python for Delphi.

8072225a9c24fcc7941fb51880b2504e.gif

tutorial -Here is a later tutorial using the Latest Techniques in Python for Delphi - use these if you have Delphi 6 and Python 2.1 or higher.

7d9c781ee0c0ef8bd12f22c940a27b8a.gif

discussion & tips - Here is a discussion and tips on python for delphi deployment issues.

dc50eb60a2094fea92162234d35d6e10.gif

animated slideshow tutorial  - Here is an animated visual viewlet demo of using the Python for Delphi components to build a Delphi app that talks to python.

5fef9bb7353374fc10921325e62182a9.gif

example and screenshot of a Delphi GUI application which uses python code for all its business logic.

return to main Andy Patterns home page

c7f76973b20d9fb4f171778450aa9a1c.gif

Latest Python Delphi techniques & developments

As of October 2001 a totally new way of talking to python we developed, within the Python for Delphi framework.  You use variants, and get smart references to python objects.

So whilst before you could create a reference to an instance to a class, in an olevariant.  Now we use variants.

Also we can instantiate classes directly (without needing to use my PyClass() function etc. etc.

Here is what Morgan, one of the authors of the components has to say:

http://groups.yahoo.com/group/pythonfordelphi/message/210

c02d58133d610a1882a7eda8a20e33db.gif

The latest way of programming in python for Delphi if you have Delphi 6 and python 2.1 or higher.

-Andy Bulka.

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 LoginMgrwhich 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 widestringsif 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);

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 1 procedure 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.btnClearSyspathToJust

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值