C#调用Python脚本的简单示例,IronPython(转载)

19 篇文章 0 订阅

转自:http://blog.csdn.net/zmq570235977/article/details/46791009


IronPython是一种在 .NET Mono上的 Python实现,由微软的 Jim Hugunin所发起,是一个开源的项目,基于微软的 DLR引擎IronPython的在CodePlex上的主页:http://ironpython.codeplex.com/

 

使用场景:

如果你的小伙伴会写Python脚本,而且已经实现大部分项目的功能不需要再用C# 实现。现在缺少窗体,此时Python+C#的组合就可以完美的结局问题啦!


示例:

借由IronPython,就可以利用.NET执行存储在Python脚本中的代码段。下面通过简单的示例说明如何应用C#调用Python脚本。


1、在VS中新建窗体项目:IronPythonDemo


2、VS的菜单中打开“Nuget程序包管理器”

 


3、搜索IronPython程序包并安装


 


4、在exe程序所在文件夹下(此例中为".\IronPythonDemo\IronPythonDemo\bin\Debug"),创建Python脚本。或将现有的脚本拷贝到该目录下。Python示例脚本实现求两个数的四则运算:

[python]  view plain  copy
  1. num1=arg1  
  2. num2=arg2  
  3. op=arg3  
  4. if op==1:  
  5.     result=num1+num2  
  6. elif op==2:  
  7.     result=num1-num2  
  8. elif op==3:  
  9.     result=num1*num2  
  10. else:  
  11.     result=num1*1.0/num2  


5、修改工程的配置文件App.config如下:

其中microsoft.scripting节点中设置了IronPython语言引擎的几个属性。

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <configSections>  
  4.     <section name="microsoft.scripting" type="Microsoft.Scripting.Hosting.Configuration.Section, Microsoft.Scripting"/>  
  5.   </configSections>  
  6.   <microsoft.scripting>  
  7.     <languages>  
  8.       <language names="IronPython;Python;py" extensions=".py" displayName="Python" type="IronPython.Runtime.PythonContext, IronPython"/>  
  9.     </languages>  
  10.   </microsoft.scripting>  
  11.     <startup>   
  12.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
  13.     </startup>  
  14. </configuration>  

6、 绘制窗体如下:



7、编写计算的函数:

[csharp]  view plain  copy
  1. private void btnCalculate_Click(object sender, EventArgs e)  
  2.         {  
  3.             ScriptRuntime scriptRuntime = ScriptRuntime.CreateFromConfiguration();  
  4.             ScriptEngine rbEng = scriptRuntime.GetEngine("python");  
  5.             ScriptSource source = rbEng.CreateScriptSourceFromFile("IronPythonDemo.py");//设置脚本文件  
  6.             ScriptScope scope = rbEng.CreateScope();  
  7.   
  8.             try  
  9.             {  
  10.                 //设置参数  
  11.                 scope.SetVariable("arg1",Convert.ToInt32(txtNum1.Text));  
  12.                 scope.SetVariable("arg2", Convert.ToInt32(txtNum2.Text));  
  13.                 scope.SetVariable("arg3", operation.SelectedIndex+1);  
  14.             }  
  15.             catch (Exception)  
  16.             {  
  17.                 MessageBox.Show("输入有误。");  
  18.             }  
  19.   
  20.             source.Execute(scope);  
  21.             labelResult.Text = scope.GetVariable("result").ToString();  
  22.         }  

8、编译运行可得计算结果(此处未做输入的检查)

 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值