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.
I will give an answer in C#, if you don’t mind. But anyway it is very similar to
VBversion. For your code accessingSquarePythonis very simpleBut for simplicity, I would modify python code a little bit, like this
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:
Notes: see VB.Net equivalent for C# ‘dynamic’ if you need to convert
dynamickeyword to VB.NET