1

I'm an IronPython novice and would like some help. I have a windows form that I have created in Visual Basic 2010 Express, which contains two text-boxes ('txtNumber' and 'txtResult') and a button ('btnSquare'). What I want to do is be able to call the below Python script ('Square.py') on clicking the button on the form;

class SquarePython:

    def __init__(self, number):

        self.sq = number*number

This script should square the number that is input in to 'txtNumber' and then output the result in to 'txtResult'. I know this is almost too simple, but I just need to know the basics. Here's what I have so far in my VB code;

Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types

Public Class Square

    Private Sub btnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquare.Click

        Dim runtime As ScriptRuntime = Python.CreateRuntime

        Dim scope As ScriptScope = runtime.ExecuteFile("Square.py")

    End Sub

End Class

Thanks in advance.

5
  • Are you getting an error? Commented Jan 3, 2013 at 12:05
  • No, the code runs as it is. But when I add the line -- Dim pythonType As PythonType = scope.GetVariable("SquarePython") -- I get an error. Commented Jan 3, 2013 at 12:34
  • Shouldn't you be looking at sq and not SquarePython, which is the class? Commented Jan 3, 2013 at 12:43
  • If I try that, I get the error "'ScopeStorage' object has no attribute 'sq'" Commented Jan 3, 2013 at 12:45
  • Ok, I am booting up my VS to recreate the issue. Commented Jan 3, 2013 at 12:48

1 Answer 1

1

I will give an answer in C#, if you don't mind. But anyway it is very similar to VB version. For your code accessing SquarePython is very simple

ScriptEngine py = Python.CreateEngine();

ScriptScope scope = py.ExecuteFile("Square.py");

dynamic square = scope.GetVariable("SquarePython");

int result = (int)square(5);

Console.WriteLine(result.sq); //prints 25 as you might expected

But for simplicity, I would modify python code a little bit, like this

class SquarePython:
    def Square(self, number):
        return number * number

So that you don't have to create an object each time you compute. Code that retrieve variable and call method to square is given below:

ScriptEngine py = Python.CreateEngine();

ScriptScope scope = py.ExecuteFile("Square.py");
//get variable and then create and object. Could be stored somewhere between computations
dynamic squareInstance = scope.GetVariable("SquarePython")(); 

int result = (int) squareInstance.Square(5);

Console.WriteLine(result);

Notes: see VB.Net equivalent for C# 'dynamic' if you need to convert dynamic keyword to VB.NET

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.