Python 開發桌面程序, 之前寫過一個使用IronPython的博客. 下面這個方案使用 delphi 作為主開發語言,通過 python4delphi 控件包將 python 作為 script 嵌入其中, Delphi + Python, 偏上加偏, pyscripter IDE 算是這種方案唯一的成果.
=================================
Delphi + Python的特點
=================================
Delphi GUI方面還是很強, 但第3方類庫太缺了.
Python正好相反, pypi的類庫太豐富了. 兩者結合在一起, 簡直是取長補短的典范.
=================================
使用 python4delphi 的開發設置
=================================
----------------------------
Step1. 安裝 python2.7 和 python4delphi VCL.
----------------------------
安裝python27: 下載並安裝 python2.7 for windows版. python27 安裝后, 在c:\WINDOWS\system32就有了 python27.dll.
安裝python4delphi: svn下載最新版的 python4delphi, http://code.google.com/p/python4delphi, 該版本已經支持 python2.7 和python3.3了. 不要從http://membres.multimania.fr/marat/delphi/python.htm 下載Python4Delphi 的installer, 它最高僅支持python 2.3.
----------------------------
Step2. 將 P4D 的定義文件加到 project.dpr
----------------------------
在Delphi project.dpr, 在uses之前,加上{$I Definition.Inc}
----------------------------
Step3. 使用pythonengince 等組件
----------------------------
設置 pythonengince 組件的屬性.
UseLastKnownVersion=False
DLLName='python27'
DllPath= 'c:\WINDOWS\system32'
----------------------------
Step4. 發布應用前, 需解決run as administrator問題,
----------------------------
(a) 需要 Microsoft.VC90.CRT 和 Microsoft.Windows.Common-Controls 信息作為資源加到delphi project中, 否則加載python的 c-extensions 模塊會報錯.
(b)創建 XP_UAC.manifest 文件, 內容見后.
(c)創建 XP_UAC.rc 文件, 內容:
1 24 XP_UAC.manifest
其中: 1-代表資源編號, 24-資源類型為 RTMAINIFEST , UAC.manifest為manifest文件名稱
(d)使用delphi的brcc32將 XP_UAC.rc編譯成 XP_UAC.res
命令: brcc32 XP_UAC.rc
(e)在Delphi project.dpr 源碼
(1)在{$R *.res} 后, 加上 {$R XP_UAC.res}
=================================
部署python4delphi的應用程序
=================================
方式1. 和開發一樣, 老老實實安裝python以及python的第3方包, 然后你的delphi程序應該就能使用.
方式2. 像dreampie, 將python環境打包, 連同exe一起發布. 有專門的py2exe,cx_freeze等制作工具.
=================================
XP_UAC.manifest 的內容
=================================
C:\WINDOWS\WinSxS 目錄查找 Windows.Common-Controls 和 Microsoft.VC90.CRT, 如果能找到並且version一致, 只需修改 PyScripter 名字. 如果沒有找到這兩個文件, 需要到微軟官網下載 Microsoft Visual C++ 2008 Redistributable Package, 並對比版本號和publicKeyToken.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="PyScripter"
type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"/>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.VC90.CRT"
version="9.0.21022.8"
processorArchitecture="x86"
publicKeyToken="1fc8b3b9a1e18e3b"
language="*"/>
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker"/>
</requestedPrivileges>
</security>
</trustInfo>
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
=================================
關於PYTHONPATH的設置
=================================
關於PYTHONPATH的設置, 可在 TPythonEngine.OnSysPathInit 事件中, 將我們自己的path加到 PythonPATH 中.
如果代碼中沒有加, 我們需要修改注冊表, 位置是 HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\PythonPath, 將我們的path加進去.
注意: TPythonEngine.OnPathInitialization 事件, 只有在注冊表沒有定義 PythonPath 才有用, 所以不推薦使用.
=================================
python4delphi 資料匯總
=================================
python4delphi 最新源碼
http://code.google.com/p/python4delphi/
Using P4D with Python 2.6, 3.0
http://code.google.com/p/python4delphi/wiki/P4DPython26
在Python 2.6, 3.0下使用P4D
http://www.cnblogs.com/babykick/archive/2011/03/25/1995970.html
Andy tips on python4delphi
http://www.atug.com/andypatterns/pythonDelphiTalk.htm
使用P4D 編寫Python Extension
http://1000copy.itpub.net/post/10379/276310
Python For Delphi---更好地協同
http://www.cnblogs.com/GarfieldTom/archive/2013/01/14/2860206.html
=================================
Delphi + Python的特點
=================================
Delphi GUI方面還是很強, 但第3方類庫太缺了.
Python正好相反, pypi的類庫太豐富了. 兩者結合在一起, 簡直是取長補短的典范.
=================================
使用 python4delphi 的開發設置
=================================
----------------------------
Step1. 安裝 python2.7 和 python4delphi VCL.
----------------------------
安裝python27: 下載並安裝 python2.7 for windows版. python27 安裝后, 在c:\WINDOWS\system32就有了 python27.dll.
安裝python4delphi: svn下載最新版的 python4delphi, http://code.google.com/p/python4delphi, 該版本已經支持 python2.7 和python3.3了. 不要從http://membres.multimania.fr/marat/delphi/python.htm 下載Python4Delphi 的installer, 它最高僅支持python 2.3.
----------------------------
Step2. 將 P4D 的定義文件加到 project.dpr
----------------------------
在Delphi project.dpr, 在uses之前,加上{$I Definition.Inc}
----------------------------
Step3. 使用pythonengince 等組件
----------------------------
設置 pythonengince 組件的屬性.
UseLastKnownVersion=False
DLLName='python27'
DllPath= 'c:\WINDOWS\system32'
----------------------------
Step4. 發布應用前, 需解決run as administrator問題,
----------------------------
(a) 需要 Microsoft.VC90.CRT 和 Microsoft.Windows.Common-Controls 信息作為資源加到delphi project中, 否則加載python的 c-extensions 模塊會報錯.
(b)創建 XP_UAC.manifest 文件, 內容見后.
(c)創建 XP_UAC.rc 文件, 內容:
1 24 XP_UAC.manifest
其中: 1-代表資源編號, 24-資源類型為 RTMAINIFEST , UAC.manifest為manifest文件名稱
(d)使用delphi的brcc32將 XP_UAC.rc編譯成 XP_UAC.res
命令: brcc32 XP_UAC.rc
(e)在Delphi project.dpr 源碼
(1)在{$R *.res} 后, 加上 {$R XP_UAC.res}
=================================
部署python4delphi的應用程序
=================================
方式1. 和開發一樣, 老老實實安裝python以及python的第3方包, 然后你的delphi程序應該就能使用.
方式2. 像dreampie, 將python環境打包, 連同exe一起發布. 有專門的py2exe,cx_freeze等制作工具.
=================================
XP_UAC.manifest 的內容
=================================
C:\WINDOWS\WinSxS 目錄查找 Windows.Common-Controls 和 Microsoft.VC90.CRT, 如果能找到並且version一致, 只需修改 PyScripter 名字. 如果沒有找到這兩個文件, 需要到微軟官網下載 Microsoft Visual C++ 2008 Redistributable Package, 並對比版本號和publicKeyToken.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="PyScripter"
type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"/>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.VC90.CRT"
version="9.0.21022.8"
processorArchitecture="x86"
publicKeyToken="1fc8b3b9a1e18e3b"
language="*"/>
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker"/>
</requestedPrivileges>
</security>
</trustInfo>
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
=================================
關於PYTHONPATH的設置
=================================
關於PYTHONPATH的設置, 可在 TPythonEngine.OnSysPathInit 事件中, 將我們自己的path加到 PythonPATH 中.
如果代碼中沒有加, 我們需要修改注冊表, 位置是 HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\PythonPath, 將我們的path加進去.
注意: TPythonEngine.OnPathInitialization 事件, 只有在注冊表沒有定義 PythonPath 才有用, 所以不推薦使用.
=================================
python4delphi 資料匯總
=================================
python4delphi 最新源碼
http://code.google.com/p/python4delphi/
Using P4D with Python 2.6, 3.0
http://code.google.com/p/python4delphi/wiki/P4DPython26
在Python 2.6, 3.0下使用P4D
http://www.cnblogs.com/babykick/archive/2011/03/25/1995970.html
Andy tips on python4delphi
http://www.atug.com/andypatterns/pythonDelphiTalk.htm
使用P4D 編寫Python Extension
http://1000copy.itpub.net/post/10379/276310
Python For Delphi---更好地協同
http://www.cnblogs.com/GarfieldTom/archive/2013/01/14/2860206.html