Home>

ironpython is a python implementation on .net and mono, initiated by Microsoft's Jim Hugunin, is an open source project,Based on Microsoft's dlr engine. ironpython's homepage on codeplex:

scenes to be used:

If your friends will write python scripts, and most of the functions of the project have been implemented, you don't need to use c#. Form is missing now,At this point, the combination of python + c#can perfectly solve the problem!

Example:

With ironpython, it works.net executes a code segment stored in a python script.The following is a simple example to show how to use c#to call a python script.

1. Create a new form item in vs:ironpythondemo

2. Open "nuget package manager" in the vs menu

3.Search ironpython package and install

4. In the folder where the exe program is located (". \ Ironpythondemo \ ironpythondemo \ bin \ debug" in this example), create a python script. Or copy the existing script to this directory.The python sample script implements four operations for two numbers:

num1=arg1
num2=arg2
op=arg3
if op == 1:
 result=num1 + num2
elif op == 2:
 result=num1-num2
elif op == 3:
 result=num1 * num2
else:
 result=num1 * 1.0/num2

5. Modify the project configuration file app.config as follows:Several properties of ironpython language engine are set in the microsoft.scripting node.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configsections>
 <section name="microsoft.scripting" type="microsoft.scripting.hosting.configuration.section, microsoft.scripting" />
 </configsections>
 <microsoft.scripting>
 <languages ​​>
  <language names="ironpython;python;py" extensions=". Py" displayname="python" type="ironpython.runtime.pythoncontext, ironpython" />
 </languages ​​>
 </microsoft.scripting>
 <startup>
  <supportedruntime version="v4.0" sku=". Netframework, version=v4.5" />
 </startup>
</configuration>

6. Draw the form as follows:

7. Write calculated functions:

private void btncalculate_click (object sender, eventargs e)
  {
   scriptruntime scriptruntime=scriptruntime.createfromconfiguration ();
   scriptengine rbeng=scriptruntime.getengine ("python");
   scriptsource source=rbeng.createscriptsourcefromfile ("ironpythondemo.py");//Set the script file
   scriptscope scope=rbeng.createscope ();
   try
   {
    //Setting parameters
    scope.setvariable ("arg1", convert.toint32 (txtnum1.text));
    scope.setvariable ("arg2", convert.toint32 (txtnum2.text));
    scope.setvariable ("arg3", operation.selectedindex + 1);
   }
   catch (exception)
   {
    messagebox.show ("Incorrect input.
");
   }
   source.execute (scope);
   labelresult.text=scope.getvariable ("result"). tostring ();
  }

8. Compile and run to get the calculation result (the input is not checked here)

c
  • Previous Analysis of the underlying principles of CodeIgniter's coherent operation
  • Next A little personal understanding of the JavaScript prototype chain