当前位置 : 主页 > 编程语言 > python >

IronPython开发Windows Form程序总结

来源:互联网 收集:自由互联 发布时间:2022-06-15
IronPython开发Windows Form程序总结 先来点评论: 1. IronPython 我本来就不看好, help文档直接拿python.org来应付, 标准库很不完整, 在2.7.3版本之前, 连datetime.strptime()都没有. 2. 使用C#的类库, 还是有

IronPython开发Windows Form程序总结

先来点评论:

1. IronPython 我本来就不看好, help文档直接拿python.org来应付, 标准库很不完整, 在2.7.3版本之前, 连datetime.strptime()都没有.

2. 使用C#的类库, 还是有点麻烦, 比如函数out 和 ref 类型参数访问不太自然 .

3. 用IronPython 开发WinForm, 限制还是很多的. 既然不能体现python的跨平台性, 直接用C#开发winform更好.


参考文章:

http://www.oracle.com/technetwork/articles/dsl/mastering-oracle-python-providers-1395759.html


==========================

环境和工具:

==========================

.Net 4.0

IronPython 2.7

Sharpdevelop 4.2 (有一个python版的Form Designer)

VS 2010 Shell + PTVS


PTVS(Python tools for visual studio), PTVS算是一个Visual Studio shell的插件吧,  它支持IronPython和CPython,  但不提供WinForm Designer.  所以说它就是一个Visual Studio版的pydev. 对于Ironpython开发, 推荐使用PTVS, 因为它能提示Net Assembly中的symbol, pydev不能.


SharpDev 基本没有python 语言智能提示, 但有一个很不错的python版Form Designer.


==========================

工具和环境的配置:

==========================

SharpDev的配置:

  1. Editor设置tab转space,  菜单 Tools/Options/Text Editor/Behavior

       选中Convert tabs into spaces  


PTVS的配置:  

   1. 环境变量 IRONPYTHONPATH: IronPython使用IRONPYTHONPATH环境变量来search 标准库和我们自己的module, 多个路径以分号分隔. (注:Jython使用JYTHONPATH环境变量, Python使用PYTHONPATH环境变量)

   2. 在PTVS中设置解析器,  菜单Tools->Options->Python Interpreter Options

    Click "Add Interpreter"

    Path :should be the path to your IronPython installation's ipy.exe or ipy64.exe

    Windows path: should be the path to your ipyw.exe or ipyw64.exe

    Architecture: is x86 or x64 depending on whether you chose ipy or ipy64

    Language version: is 2.7

    Path Environment Variable: is IRONPYTHONPATH

   3. PTVS Project 在 solution exploerer有个Search Path节点, 用来增加项目自身的Search path, 即project specific IRONPYTHONPATH


==========================

开发经验总结:

==========================

1. IronPython 2.7 在 catch CLR Exception时, 得到的exception, 其属性message 类型居然是Clr Exception对象, 比如OdbcException. 怎么也应该是 str 类型啊, 坑爹啊. 要得到真正的message字符串, 使用ex.message.ToString() 或 ex.ToString().


2. C#方法可以有ref和out参数说明, ironpython调用这样的C#, 有点麻烦. 参考

http://stackoverflow.com/questions/8397421/ironpython-defining-a-variable-of-specific-type


3. 使用 SharpDev 创建一个WinForm project( project 名为 xxx.pyproj ), 做界面设计. 然后创建一个PTVS的project(名为xxx_vs.pyproj), 手动将 SharpDev 项目xxx.pyproj中的所有文件加到这个项目中, 同时配置好reference. SharpDevelop 专司创建project和设计界面之职, PTVS专司python编码. 另外, PVTS 1.5 还不能在debug模式下,  watch一个python变量.


4. 要引用.Net Assembly, 需在 py 代码中通过clr.AddReference('YourAssemblyName').

   如果你的Assembly不在GAC中, 可以通过如下方式做加载.

clr.AddReferenceToFileAndPath(“D:/oracle/product/11.2.0/server/odp.net/bin/4/Oracle.DataAccess.dll”)


5. 代码有问题时,  sharpdev 运行程序就会直接关掉运行进程, 根本没有机会看的输出信息. 这时候, 可以采用调试的方式运行, 可获得更多的信息. 或者使用PTVS运行一下.


6. 使用IronPython的Tools\Scripts\pyc.py, 可将ironpython脚本转成win exe或console exe程序, 示例:

ipy.exe pyc.py /target:winexe /out:my_app  /main:Program.py Form1.py Form2.py


7. 关于数据库访问

当然可以使用ADO.Net, 但我更喜欢Python DB API, pypyodbc + ODBC driver 是我的选择.

 pypyodbc  http://code.google.com/p/pypyodbc/


8.目前SharpDevelop的FormsDesigner还有部分的限制, 比如不支持timer; 不支持Form resources, 所以Tool Button也不要有image. 即使能设计时可以, 运行时也会碰到不能resource中加载, 下一个条目是解决的办法. 详见:

http://community.sharpdevelop.net/blogs/mattward/archive/2009/05/12/IronPython2FormsDesigner.aspx

The IronPython forms designer is not yet complete and the following are some of the known limitations.

    No support for project or local form resources.

    No support for icons.

    Incomplete support for ToolStripItems and menu strips.

    Incomplete support for ListViewItems.

    No support for TreeViewItems.

    Incomplete support for non-visual components (e.g. Timers).

    Controls needed to be fully namespace qualified.


9. 因为不能加载resource中的内容, 那该加载image等resource呢?

摘自: http://stackoverflow.com/questions/589999/accessing-embedded-resources-in-ironpython    

IronPython is a dynamic scripting language; it is interpreted at runtime from the script files themselves, rather than being compiled into an assembly. Since there is no compiled assembly, you cannot have an embedded resource. Here are two ways of adding an icon to a form in IronPython:


First, you could include the icon as a loose file alongside the python scripts. You can then create the icon object by passing the icon filename to the System.Drawing.Icon constructor. Here is an example of this scenario, where the main python script and the icon are deployed in the same directory. The script uses the solution found here to find the directory.

import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')

import os
import __main__
from System.Drawing import Icon
from System.Windows.Forms import Form

scriptDirectory = os.path.dirname(__main__.__file__)
iconFilename = os.path.join(scriptDirectory, 'test.ico')
icon = Icon(iconFilename)

form = Form()
form.Icon = icon
form.ShowDialog()

Alternatively, you can load an icon that is included as an embedded resource in a .NET assembly that is written in C#, for instance.

import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')

from System.Drawing import Icon
from System.Reflection import Assembly
from System.Windows.Forms import Form

assembly = Assembly.LoadFile('C:\\code\\IconAssembly.dll')
stream = assembly.GetManifestResourceStream('IconAssembly.Resources.test.ico')
icon = Icon(stream)

form = Form()
form.Icon = icon
form.ShowDialog()





上一篇:记一个奇怪的python异常处理过程
下一篇:没有了
网友评论