In this example you can see how to pass parameter from Windows Forms UI to a Python function using IronPython.
Here's the step by step guide:
-
Create a Windows Forms application.
-
Install IronPython nuget package.
-
Create a Code.Py file in your project, set the Copy to output directory to "Copy Alwys", and use the following content:
def hello(name): result = 'Hello, {}!'.format(name) return(result)
-
Add
using IronPython.Hosting;
statement to the using statements. -
Drop a
TextBox
on the form (textBox1) -
Drop a
Button
on the form (button1) -
Double click on button and add the following code for the click handler:
private void button1_Click(object sender, EventArgs e) { var engine = Python.CreateEngine(); var scope = engine.CreateScope(); //You can also load script from file var source = engine.CreateScriptSourceFromFile( Path.Combine(Application.StartupPath, "Code.py")); //You can load from string //var source = engine.CreateScriptSourceFromString( // "def hello(name):" + "\n" + // " result = 'Hello, {}!'.format(name)" + "\n" + // " return(result)", // Microsoft.Scripting.SourceCodeKind.Statements); var compiled = source.Compile(); compiled.Execute(scope); dynamic hello = scope.GetVariable("hello"); var result = hello(textBox1.Text); MessageBox.Show((string)result); }
-
Press F5 to Run the application, enter a text, and click on the button. The function will run and you will see a message box saying hello: