python4delphi 使用

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:WINDOWSsystem32就有了 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:WINDOWSsystem32'   
   
        
----------------------------
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:WINDOWSWinSxS 目录查找 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_MACHINESOFTWAREPythonPythonCore2.7PythonPath, 将我们的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
原文地址:https://www.cnblogs.com/harrychinese/p/python4delphi.html