[原创]Python For Delphi 示例 (最好的Python GUI实现方法)

260 views
Skip to first unread message

samson

unread,
Aug 4, 2007, 3:22:53 AM8/4/07
to python.cn
最近用Python For Delphi有了一段时间,感觉这么好的东西不与大家分享简直是暴敛天物了,而且前一段看大家讨论GUI的问题相当多,为
了让大家少走点弯路,所以萌发了写此文的念头。

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


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程序在同一个目录下。代码如下:

# -*- coding: utf-8 -*-


class MyP4DStrList(list):
def CopyFrom(self,SrcList):
self.Clear()
for i in SrcList:
self.append(i)
def Clear(self):
for x in range(len(self)): del self[0]
def GetItem(self,index):
return self[index]
def RemoveItem(self,index):
del self[index]
def AppendStr(self,StrToAppend):
self.append(StrToAppend)
def RemoveStr(self,StrToRemove):
self.remove(StrToRemove)

IntfListDemo=MyP4DStrList()


def main():
IntfListDemo.append('hello')
IntfListDemo.append('world')
IntfListDemo.append('with')
IntfListDemo.append('P4D')

if __name__ == '__main__':
main()

Zoom.Quiet

unread,
Aug 4, 2007, 4:19:39 AM8/4/07
to pyth...@googlegroups.com
On 8/4/07, samson <yan_xi...@hotmail.com> wrote:
> 最近用Python For Delphi有了一段时间,感觉这么好的东西不与大家分享简直是暴敛天物了,而且前一段看大家讨论GUI的问题相当多,为
> 了让大家少走点弯路,所以萌发了写此文的念头。
>
赞!!收录!
http://wiki.woodpecker.org.cn/moin/PyForDelphi


--
'''Time is unimportant, only life important!
过程改进的目标不是高品质产品,而是促生靠谱的人的组织!
'''
http://zoomquiet.org
blog@http://blog.zoomquiet.org/pyblosxom/
wiki@http://wiki.woodpecker.org.cn/moin/ZoomQuiet
or http://202.108.44.62/moin/ZoomQuiet
scrap@http://floss.zoomquiet.org
douban@http://www.douban.com/people/zoomq/
____________________________________
Pls. use OpenOffice.org to replace M$ Office.
http://zh.openoffice.org
Pls. use 7-zip to replace WinRAR/WinZip.
http://7-zip.org/zh-cn/
You can get the truely Freedom 4 software.

Message has been deleted

unread,
Aug 4, 2007, 11:01:02 AM8/4/07
to python.cn
请问有更多的用过吗?

黄毅

unread,
Aug 4, 2007, 10:16:46 PM8/4/07
to pyth...@googlegroups.com
delphi 做 gui 确实是无敌了,不过不跨平台吧,好像有个kylix不知效果如何。
另外 P4D 可以用于 c++builder 么?

 
On 8/4/07, samson <yan_xi...@hotmail.com> wrote:
最近用Python For Delphi有了一段时间,感觉这么好的东西不与大家分享简直是暴敛天物了,而且前一段看大家讨论GUI的问题相当多,为
了让大家少走点弯路,所以萌发了写此文的念头。

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


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');

samson

unread,
Aug 4, 2007, 10:45:29 PM8/4/07
to python.cn
下载:
http://mmm-experts.com/Downloads.aspx?ProductId=3

注意,安装文件中的教学是针对老模式的,比较繁琐,但是也有一定的参考价值。
下面的链接是较新版本的教学。P4D新的调用模式非常完美!
http://www.atug.com/andypatterns/PythonDelphiLatest.htm

支持Window和Linux平台。
CBuilder没见站点上说,用google搜一下吧,看有没有第三方控件。

tocer

unread,
Aug 5, 2007, 1:42:16 AM8/5/07
to pyth...@googlegroups.com
使用 wine 可以运行部分 delphi做的程序,也算是跨平台吧。P4D可以用在c++builder上

黄毅 wrote::

Harry

unread,
Aug 6, 2007, 10:41:41 PM8/6/07
to python.cn
试用了一下, 确实是无敌啊! 赞!

On 8月5日, 下午1时42分, tocer <tocer.d...@gmail.com> wrote:
> 使用 wine 可以运行部分delphi做的程序,也算是跨平台吧。P4D可以用在c++builder上
>
>
>
> 黄毅 wrote::
> >delphi做 gui 确实是无敌了,不过不跨平台吧,好像有个kylix不知效果如何。
> > 另外 P4D 可以用于 c++builder 么?- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Reply all
Reply to author
Forward
0 new messages