IronPython開發Windows Form程序總結


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()


免責聲明!

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



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