Python For Delphi---更好地協同(續)


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

怎么樣,是不是看起來也很方便?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM