IronPython: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Brian Wilson (talk | contribs)
Line 50: Line 50:
But do I have to preinstall IronPython from the MSI or can I just run ipyw?
But do I have to preinstall IronPython from the MSI or can I just run ipyw?
NO. I don't I did the MSI install and then copied the file tree into a convenient place. I was able to run it no problems.
NO. I don't I did the MSI install and then copied the file tree into a convenient place. I was able to run it no problems.
== Connecting a web form to IronPython ==
I created a new solution.
I created a VS2010 Python project.
I created a Windows Form project and designed a simple form; no handlers.
The form has a text box and two buttons, labeled "Install" and "Cancel".
Now I need to connect the controls in the form to the Python code.
=== SUCCESS ===
I followed the instructions in this book: Professional IronPython (ISBN 978-0-470-54859-2) Chapter 16.
Per their instructions,
* I added ipyw.exe to the "solution" as an "existing project" and set it as the startup project.
* On the project properties, I set Arguments to launch my python script (-D IPyInstall.py) and set working directory to the folder containing the Python.
Now when I run the project, it builds the C# form and fires up ipyw.exe. '''Very cool'''. My Python code can directly access the form properties. Here is how I connect the Cancel button to a Python handler. So clean and simple!
The Python code looks like this:
<pre>
import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import *
# NB: the Build property settings for the view project cause output to go to the top level folder.
clr.AddReference('InstallerView.dll')
from InstallerView import InstallerForm
def OnClick(*args):
  form.Close()
       
form = InstallerForm()
form.buttonCancel.Click += OnClick
form.Show()
Application.Run(form)
</pre>

Revision as of 17:28, 9 June 2011

Okay, so I want to write an installer for a .Net 3.5 program. I want to write it in Python because I think Python is a great language.

I tried valiantly to get py2exe to build me a Python 2.6 executable using wxPython. I followed all the instructions in the py2exe 5.2 section, I tried many other variations, I failed. Can't load msvcp90.dll.

Maybe if I use IronPython, I think I can get things going. Turns out this is true, I can!

IronPython 2.6.1 will run on .Net 2.0 SP1, and that means it's good on Windows XP or Windows 7. I don't care about Windows Vista. (No one does!)

I installed both the 2.6.1 for .Net 2 and the 2.7 versions of IronPython. (The 2.7 version requires .Net 4.0. Later for that!)

The IronPython tutorial uses the text driven console. 2.7 supports a Visual Studio 2010 development environment. I wish Komodo supported IronPython.

I think I will develop in VS2010 with 2.7 and then backport to 2.6

If you install VS2010 and then install IronPython 2.7, it will install the VS support. Ignore the docs that say to click on the VSIX file, there isn't one.

Sample app

import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import *

class MyForm(Form):
    def __init__(self):
         self.quit_button = Button()
         self.quit_button.Text = 'Quit'
         self.Controls.Add(self.quit_button)
 
def OnClick(*args):
   form.Close()
   return

form.Text = "My beloved sample app"
form.quit_button.Click += OnClick
form.Show()

Application.Run(form)

Running it from a command line

ipyw.exe Program.py

But does it run in .Net 2 environment? Well, sure it does!

But do I have to preinstall IronPython from the MSI or can I just run ipyw? NO. I don't I did the MSI install and then copied the file tree into a convenient place. I was able to run it no problems.

Connecting a web form to IronPython

I created a new solution. I created a VS2010 Python project. I created a Windows Form project and designed a simple form; no handlers.

The form has a text box and two buttons, labeled "Install" and "Cancel". Now I need to connect the controls in the form to the Python code.

SUCCESS

I followed the instructions in this book: Professional IronPython (ISBN 978-0-470-54859-2) Chapter 16.

Per their instructions,

  • I added ipyw.exe to the "solution" as an "existing project" and set it as the startup project.
  • On the project properties, I set Arguments to launch my python script (-D IPyInstall.py) and set working directory to the folder containing the Python.

Now when I run the project, it builds the C# form and fires up ipyw.exe. Very cool. My Python code can directly access the form properties. Here is how I connect the Cancel button to a Python handler. So clean and simple!

The Python code looks like this:

import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import *

# NB: the Build property settings for the view project cause output to go to the top level folder.
clr.AddReference('InstallerView.dll')
from InstallerView import InstallerForm

def OnClick(*args):
   form.Close()
        
form = InstallerForm()
form.buttonCancel.Click += OnClick
form.Show()
Application.Run(form)