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中寫下面的代碼:
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
怎么樣,是不是看起來也很方便?