Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Referring to a Windows form with IronPython

Posted on 2011-03-18
8
Medium Priority
?
625 Views
Last Modified: 2013-11-07
I'm trying to integrate Iron Python into a .Net application (VS2008, Framework 3.5, OS XP2 SP3).  I've read several samples of how to create a form with controls using IronPython.  However, I would prefer to create the form in the C# application, and have IronPython refer to that form instead.

I'm guessing that this action is possible after embedding the IronPython, but it's not clear to me how.

Thanks in advance...
0
Comment
Question by:cshore12
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 1200 total points
ID: 35287282
Since you already have an existing C# app, st sounds like in your case you'd want to either embed (host) IronPython in your app, or compile a separate Python file into an assembly and link in the dll.

Embed it like this, then pass in objects to the python environment. In the example below I create the python engine local to the Form1 class itself, then pass "this" into as form1. You could create the Python engine at any scope, perhaps a program wide static variable.

The other method, compiling the Python into an assembly, is done with pyc, the Python .NET compiler, but I've no experience with it. http://www.ironpython.net/documentation/dotnet/
// Untested
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace TestPython
{
   public partial class Form1 : Form
   {
      public ScriptEngine pythonEngine = Python.CreateEngine();
      public ScriptScope pythonScope;
      
      public Form1()
      {
         InitializeComponent();
      
         pythonScope = pythonEngine.CreateScope();
         pythonScope.SetVariable("form1", this); // now the form is availabe to the Python environment
      }

      private void button1_Click(object sender, EventArgs e)
      {
         ScriptSource script = pythonEngine.CreateScriptSourceFromString(@"... some python code here ...");
         script.Execute(pythonScope);
      }
   }
}

Open in new window

0
 

Author Comment

by:cshore12
ID: 35475888
Your suggestion looks very useful.  However, I'm not quite sure where to take it from here.

I've changed the button click code to the following:

            CompiledCode code = Eng.CreateScriptSourceFromFile("c:\\Other\\Python\\Scoping.py").Compile();
            code.Execute(pythonScope);

and Scoping.py is very basic:

import sys, os

log = open('C:\\Other\\pyOut.txt', 'w')
print >> log, "33"
#form1.txtScoping.Text = "27"

The form contains a text box and command button.  When I press the command button with the last line commented out, "33" is correctly written to the log file.  However, when I include the last line, the program fails.

The ultimate goal is to include classes with methods in the Python file.  The code would then look like this:

class A:
    def B(args*):
       form1.txtScoping.Text = "27"
       ...

In other attempts, I've used 'GetVariable' to get the class name and 'GetMember' to get the method.  However, I've had problems with the subsequent Invoke method (i.e, calling Invoke from an ObjectOperations object.

Thanks in advance...
0
 

Author Closing Comment

by:cshore12
ID: 35802189
Never addressed my follow-on comments
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 35802290
Sorry mate, I got busy and forgot to come back to this. I meant to do a test project and make it work but never got around to it. If I do, I will update the thread here.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question