IronPython: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Brian Wilson (talk | contribs)
 
(3 intermediate revisions by the same user not shown)
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 Windows 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!
'''Deleting references''' -- because I am targeting a .Net 2.0 machine, I changed the platform in the form to .Net 2.0. This meant I had to delete the references that were automatically added when I created the project. Delete the ones that are not available -- they show in the references list with yellow icons. They include System.Core, System.Xml.Linq and System.Data.DataSetExtensions
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>
=== Redirecting output ===
I want to be able to use simple 'print' statements in Python and have the output go to the Windows form textbox. That way my console app can run without modification.
== Additional help ==
I am only interested in the basics of using VS2010 as a form designer and then connecting the forms to my python code.
There is a nice tutorial included in the MSI package for IronPython. I suggest you start there. Then I suggest one of the excellent IronPython books as your next step. Either the Wrox book (Professional IronPython) or the one from Manning (IronPython in Action).

Latest revision as of 17:43, 10 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 Windows 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!

Deleting references -- because I am targeting a .Net 2.0 machine, I changed the platform in the form to .Net 2.0. This meant I had to delete the references that were automatically added when I created the project. Delete the ones that are not available -- they show in the references list with yellow icons. They include System.Core, System.Xml.Linq and System.Data.DataSetExtensions

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)

Redirecting output

I want to be able to use simple 'print' statements in Python and have the output go to the Windows form textbox. That way my console app can run without modification.

Additional help

I am only interested in the basics of using VS2010 as a form designer and then connecting the forms to my python code.

There is a nice tutorial included in the MSI package for IronPython. I suggest you start there. Then I suggest one of the excellent IronPython books as your next step. Either the Wrox book (Professional IronPython) or the one from Manning (IronPython in Action).