Python For Delphi 示例

Python For Delphi 示例

samson <yan_xiao_song@hotmail.com>            hide details    3:22 pm (50 minutes ago) 
        reply-to                python-cn@googlegroups.com      
        to              "python.cn" <python-cn@googlegroups.com>        
        date            Aug 4, 2007 3:22 PM     
        subject         [CPyUG:29962] [原创]Python For Delphi 示例 (最好的Python GUI实现方法)      
        mailed-by               googlegroups.com
最近用Python For Delphi有了一段时间,感觉这么好的东西不与大家分享简直是暴敛天物了,而且前一段看大家讨论GUI的问题相当多,为 了让大家少走点弯路,所以萌发了写此文的念头。

因时间有限,在此只做一个简要的实例,在该实例中,可以通过delphi操作Python中的list对象。其实P4D功能十分强大,远不止这些,还可 以用Delphi写出供Python调用的模块,这样的模块性能可是相当的高哦。 希望能够借此文抛砖引玉,勾起大家使用P4D的愿望。


1.1. 步骤
0。安装delphi7,安装python25
1。安装P4D控件
2。创建一个窗体,放置PythonEngine,PythonGUIInputOutput,Memo三个控件

3。PythonEngine的IO属性指到PythonGUIInputOutput,PythonGUIInputOutput的Output属性指到Memo

4。在主窗体的FormActivate事件中,添加如下代码(注意还需要增加Uses):


uses
 VarPyth;

procedure TForm1.FormActivate(Sender: TObject);
var
 PyModule: variant;
 i: integer;
begin
 PyModule:=Import('hello');
 memo1.Lines.Add(Format('模块初始化时接口对象长度:%s',
[PyModule.IntfListDemo.Length]));
 PyModule.main();
 memo1.Lines.Add(Format('调用Python后接口对象长度:%s',
[PyModule.IntfListDemo.Length]));
 memo1.Lines.Add('接口对象内容');
 for i:=0 to PyModule.IntfListDemo.Length-1 do
 begin
   memo1.Lines.Add(PyModule.IntfListDemo.GetItem(i));
 end;

 for i:=PyModule.IntfListDemo.Length-1 downto 0 do
 begin
   PyModule.IntfListDemo.RemoveItem(i);
   memo1.Lines.Add(Format('操纵Python对象后,接口对象长度:%s',
[PyModule.IntfListDemo.Length]));
 end;

 PyModule.IntfListDemo.AppendStr('重新');
 PyModule.IntfListDemo.AppendStr('初始化');
 PyModule.IntfListDemo.AppendStr('Python');
 PyModule.IntfListDemo.AppendStr('变量');

 memo1.Lines.Add('接口对象内容');
 for i:=0 to PyModule.IntfListDemo.Length-1 do
 begin
   memo1.Lines.Add(PyModule.IntfListDemo.GetItem(i));
 end;
end;
5。创建hello.py程序,与delphi程序在同一个目录下。代码如下:

切换行号显示
   1 # -*- coding: utf-8 -*-
   2 class MyP4DStrList(list):
   3        def CopyFrom(self,SrcList):
   4                self.Clear()
   5                for i in SrcList:
   6                        self.append(i)
   7        def Clear(self):
   8                for x in range(len(self)): del self[0]
   9        def GetItem(self,index):
  10                return self[index]
  11        def RemoveItem(self,index):
  12                del self[index]
  13        def AppendStr(self,StrToAppend):
  14                self.append(StrToAppend)
  15        def RemoveStr(self,StrToRemove):
  16                self.remove(StrToRemove)
  17
  18 IntfListDemo=MyP4DStrList()
  19
  20
  21 def main():
  22        IntfListDemo.append('hello')
  23        IntfListDemo.append('world')
  24        IntfListDemo.append('with')
  25        IntfListDemo.append('P4D')
  26
  27
  28
  29 if __name__ == '__main__':
  30        main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值