Solved

Referring to a Windows form with IronPython

Posted on 2011-03-18
8
610 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
8 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 400 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

803 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