IronPython 使用注意事项:
编辑器所在主线程模式只能使用STA线程
不支持this指代eg:Scope.Set
private void btn_Cal_Click(object sender, EventArgs e)
{
tb_Result.Text = "";
ScriptRuntime scriptRuntime = ScriptRuntime.CreateFromConfiguration();
ScriptEngine rbEng = scriptRuntime.GetEngine("python");
ScriptSource source = rbEng.CreateScriptSourceFromFile("IronPythonDemo.py");//设置脚本文件
ScriptScope scope = rbEng.CreateScope();
try
{
//设置参数
scope.SetVariable("arg0", this);
scope.SetVariable("arg1", Convert.ToInt32(txtNum1.Text));
scope.SetVariable("arg2", Convert.ToInt32(txtNum2.Text));
scope.SetVariable("arg3", operation.SelectedIndex + 1);
}
catch (Exception ex)
{
tb_Result.Text += "\r\nErrorMessage:" + ex.Message;
}
try
{
source.Execute(scope);
labelResult.Text = scope.GetVariable("result").ToString();
}
catch (Exception ex)
{
tb_Result.Text += "\r\nErrorMessage:" + ex.Message;
}
}