Solved

Referring to a Windows form with IronPython

Posted on 2011-03-18
8
618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

627 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