?
Solved

Referring to a Windows form with IronPython

Posted on 2011-03-18
8
Medium Priority
?
621 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 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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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

719 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