65.9K
CodeProject 正在变化。 阅读更多。
Home

IronTextBox2

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.23/5 (9投票s)

2008 年 5 月 27 日

MIT

3分钟阅读

viewsIcon

64254

downloadIcon

1178

一个用于 IronPython 2.0B2+ 的 CLI 控件。

引言

在撰写本文时,IronPython 版本 2 (IP2) 经过数月的 alpha 构建后,现在处于 beta 阶段,因此我认为启动 IronTextBox 的下一个版本以与 IP2 配合使用是足够安全的。如果您正在使用 IP1.1,这里是可与 IP1.1 配合使用的 IronTextBox 版本。许多新的 IronPython 用户经常问如何在 Form 中托管 IronPythonEngine,或者是否有简单的开箱即用代码可供查看。我最初创建 IronTextBox 是在 IronPython 相当新的时期。从那时起,微软开发了许多改进和其他开发工具来帮助剖析 Python 代码,例如 IronPython Studio。 IronTextBox 仍然非常方便,因为您可以快速浏览到运行时的一个 Python 文件,并在 CLI 中运行它。

IronTextBox.dll 库

IronTextBox 被设计成一个类似 CLI 的控件,可以拖放到 Form 或其他 .NET 控件中。

  • UIIronTextBox:是主要的命名空间
  • UIIronTextBox.IPEWrapper:重写的 Stream 类,用于处理 IP 输出
  • UIIronTextBox.Paths:易于访问的常用路径
  • UIIronTextBox.Utils:这里不多,只是早期调试的一些实验

IronTextBox.dll Classes

IronTextBox 2 有什么不同?

与 IronPython 1.1 相比,变化太多了,但在我看来,最大的变化之一是 PythonEngine 和运行 Python 代码。 IP2 更加以 Microsoft.Scripting 命名空间为中心。因此,您不能轻易地创建一个 PythonEngine 对象并开始运行。在 IronTextBox 中,我创建了三个方法,这些方法几乎处理了需要发生的主要事情,并且可以轻松地替换 IronTextBox 版本 1 的代码

/// Executes the Python file within the IronTextBox environment.
/// A nice way to quickly get a Python module in CLI to test or use.
object DoIPExecuteFile(string pyfile)
{
    ScriptSource source = engine.CreateScriptSourceFromFile(pyfile);
    return source.Execute(scope);
}

/// Executes the code in SourceCodeKind.SingleStatement to fire the command event
/// Use DoIPEvaluate if you do not wish to fire the commandevent
object DoIPExecute(string pycode)
{
    ScriptSource source = engine.CreateScriptSourceFromString(pycode, 
                          SourceCodeKind.SingleStatement);
    return source.Execute(scope);
}

/// Executes the code in SourceCodeKind.Expression not to fire the command event
/// Use DoIPExecute if you do wish to fire the commandevent
object DoIPEvaluate(string pycode)
{
    ScriptSource source = engine.CreateScriptSourceFromString(pycode, 
                          SourceCodeKind.Expression);
    return source.Execute(scope);
}

以下是每个文件中新增功能的一些详细信息

//Paths.cs - version 2.0.2.0b
//
// WHAT'S NEW:
//      - Updated IronPython_Tutorial to point to IronPython 2.0B2 path
//      - Added Python25Dirs 
////////////////////////////////////
//IronTextBox.cs - version 2.0.2.0b
//WHAT'S NEW: 
//      -IronPython 2.0B2 Support
//      -Removed ParsetheText() since IronPython.Compiler.Ast removed in IP2SimEnter();
//      -Added DoIPExecuteFile(), DoIPExecute(), DoIPEvaluate()
////////////////////////////////////

用法

使用 IronTextBox 时,您的可执行文件目录必须包含

  • IronTextBox.dll
  • IronPython.dll
  • IronPython.Modules.dll
  • Microsoft.Scripting.Core.dll
  • Microsoft.Scripting.dll

只需在您的项目中添加对这些文件的引用,并将 Copy Local 的属性设置为 True。所有这些文件(当然除了 *IronTextBox.dll* 之外)都可以在 IronPython 2.0B2 下载中找到。

提示:如果您收到以下异常,请尝试包含上述文件:“Microsoft.Scripting.InvalidImplementationException : Type 'IronPython.Runtime.PythonContext' doesn't provide a suitable public constructor or its implementation is faulty.”(Microsoft.Scripting.InvalidImplementationException:类型“IronPython.Runtime.PythonContext”未提供合适的公共构造函数,或者其实现存在缺陷。)

运行此演示

  1. 请确保您已下载并解压 IronPython 2.0B2 (或者尝试访问 IronPython 的主网站以获取最新版本)。仅供参考:我已将 IronPython 脚本文件夹硬编码到 Environment.SpecialFolder.MyDocuments + @"\Visual Studio 2008\Projects\IronPython-2.0B2\Tutorial" 在项目中。 但是,我已经实现了一个 OpenFileDialog 来纠正任何不正确的路径。
  2. 下载并解压缩本文的 .NET 解决方案和项目文件。
  3. 从 MS VC# 2008 解决方案资源管理器中,通过浏览到 *IronPython.dll*、*IronPython.Modules.dll*、*Microsoft.Scripting.Core.dll*、*Microsoft.Scripting.Core.dll* 和 *Microsoft.Scripting.dll* (位于您解压缩 IronPython 下载的位置)来添加引用。

如果您是 IronPython 的新手,您可以运行 IronTextBox 命令 'runfile',然后浏览到 *IronPython-2.0B2\Tutorial\first.py*;请注意,您可以如何使用 *add*、*factorial* 和 *print hi*。您可以运行 'dir()' 命令来查看可用的内容。

结论

IronTextBox v2 可以放到 Form 中,以提供带有 IronPython 2.0B2 的命令行界面 (CLI)。 以前版本的 IronTextBox 适用于 IP1.1。 IronTextBox 还展示了如何使用 IronPython 2.0B2。

参考文献

更新

  • 5/26/08
    • 文章和 IronTextBox 版本 2.0.2.0 已发布。
  • 6/10/08
    • 使用 IronPython 2.03B 编译和测试。
    • 将源许可证从 GNU 更新为 Expat/MIT。
© . All rights reserved.