Solved

Referring to a Windows form with IronPython

Posted on 2011-03-18
8
606 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now