先上相關資源的下載吧:
python4delphi:
主頁:
JAVA和delphi,http://code.google.com/p/python4delphi/
下載:
svn checkout http://python4delphi.googlecode.com/svn/trunk/ python4delphi-read-only
現在已支持到XE2.
python執行效率,必看(作者):
http://www.atug.com/andypatterns/pythonDelphiTalk.htm
下面要示范的就是在XE2下完成.其實源碼檢出后,里面有30多個示例,幾乎涵蓋了Python4Delphi的所有方面.好吧,我們下面做個簡單的加法計算器,主要是演示二者之間的參數傳遞.
當然,需要在Delphi中先安裝上PythonForDelphi控件包,安裝不麻煩,可參考上述資料的說明文檔.
python與JAVA、在XE2中新建一個工程,然后在窗口中依次放上一個TPythonEngine,三個TPythonDelphiVar,TPythonDelphiVar的VarName分別設置為Num1,Num2,Result.再放上三個LabelEdit,分別命名為edtNum1,edtNum2,edtResult.
上代碼:unit FfrmMain;interfaceuses 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)
python和delphi哪個好,在執行按鈕中填加代碼:pyEngine.ExecStrings(memInput.Lines);
當然,可以直接執行上面的Python代碼.
在edtNum1中輸入一個數字,在edtNum2中輸入一個數字,點擊按鈕,執行python腳本后就可以在edtResult中返回計算結果.
注意:
delphi編程。Result.Value=int(Num1.Value)+int(Num2.Value)
TPythonDelphiVar傳人的是字符類型,所以要轉換為int后再相加,否則是字符串相加.
這樣,我們就完成了Delphi傳遞參數到Python,Python執行完畢后將結果再返回給Delphi的演示.好了,我們可以好好利用Python,將它很好地嵌入到Delphi中了.
如果要傳遞更復雜的參數怎么辦?我想,或許可以將要傳遞的參數JSON化,然后將JSON作為參數在二者之間相互傳遞,這樣可以完成更復雜的功能.
delphi集成python,附上Python JSON文檔:
http://docs.python.org/2/library/json.html
Delphi JSON之SuperObj:
http://www.progdigy.com/?page_id=6
delphi python。http://code.google.com/p/superobject/
Delphi通過PythonForDelphi變量來和Python交換數據可以,有沒有別的辦法了呢?有,可以像COM一樣來調用Python模塊的變量和函數,這看起來好像能更酷一些 :-)
感謝samson,是他的一篇文章使我學習到了這個方法,并且很熱心地給予了指教!
廢話少說,先上Python代碼(hello.py,放到程序目錄下):strPython='Hello,This is a python string !'dicPython={'StringInfo':'Hello,This is a python string !'}lstPython=list('Hello,This is a python string !')def SayHello(s): return 'Hello,'+s
Delphi語言、上面是簡單的示例,有變量和函數,我們看看在Delphi中怎樣來調用.
在Delphi中寫下面的代碼:var PyModule: variant;.... PyModule := Import('hello'); //測試Python變量傳遞 Memo1.Lines.Add(PyModule.strPython); Memo1.Lines.Add(PyModule.dicPython); Memo1.Lines.Add(PyModule.lstPython); Memo1.Lines.Add(PyModule.SayHello('Garfield'));
執行后,在Delphi的Memo1中將看到下面的內容:Hello,This is a python string !{'StringInfo': 'Hello,This is a python string !'}['H', 'e', 'l', 'l', 'o', ',', 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'p', 'y', 't', 'h', 'o', 'n', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', '!']Hello,Garfield
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态