PyForDelphi

Python GUI开发

源自列表Python For Delphi 示例 (最好的Python GUI实现方法)

::-- ZoomQuiet [2007-08-04 08:57:46]

CPUG联盟::

CPUG::门户plone

BPUG

SPUG

ZPUG

SpreadPython Python宣传

1. Python For Delphi 示例

{{{samson <[email protected]> hide details 3:22 pm (50 minutes ago)

}}} 最近用Python For Delphi有了一段时间,感觉这么好的东西不与大家分享简直是暴敛天物了,而且前一段看大家讨论GUI的问题相当多,为 了让大家少走点弯路,所以萌发了写此文的念头。

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

1.1. 步骤

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;

   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()

2. 反馈

PyForDelphi (last edited 2009-12-25 07:14:00 by localhost)