IronPython Hello World Tutorial
As some of you may have heard IronPython 1.0 has been released. If you don’t’ know what IronPython is, it’s:
a new implementation of the Python programming language running on .NET. It supports an interactive console with fully dynamic compilation. It is well integrated with the rest of the .NET Framework and makes all .NET libraries easily available to Python programmers, while maintaining full compatibility with the Python language.
The cool thing about IronPthon is that it also works with the Windows Presentation Foundation which will be released when Windows Vista is released and available for Windows XP and Windows Vista.
I thought that this was pretty interesting so I thought I’d try playing with it and write a quick tutorial.
Requirement
For this tutorial you need the following:
- Microsoft Windows XP (may work with 2000)
- .Net runtime 2.0
- IronPython
- Microsoft Expression Interactive Designer Beta
- Visual Studio 2005 Express
- .Net 3.0 Beta
For this tutorial I has assumed that you have installed IronPython to:
C:\IronPython
The GUI
Once you have everything installed load up Microsoft Expression Interactive Designer (MEID from now on) or sparkle as it used to be called and close the Welcome Screen. You might notice that MEID is very slow, on my system the program just seems to c-r-a-w-l, but it does allow you to do some pretty neat things.
After it has finished loading and clearing the Welcome screen you should be left with a blank canvas. The canvas is too large for the Hello World program we’re creating so resize the canvas until it has reached a suitable size.
Then with the canvas selected, select the Background item in the Appearance pallet and set the background of the canvas (our window) to something suitable using the colour picker below. I selected a nice dark shade of green.

The next step is to add a button to our window. To do this select the Button item in the Library palette and draw the button onto the canvas.


Next we want to do change the text on the button to be “Press me!” to do that select the Text tool in the Tools pallet (it is the solid black letter A) and click on the button. You can now edit the text, so delete the “button” text and type “Press me!” Select the selection tool in the Tools pallet (the black arrow) when you are done.
The next thing to do is save your project (to whatever folder you wish) and then export your xaml code (File | Export XAML) to:
C:\IronPython\Projects\HelloWorld\hw.xaml
The Code
The reference for almost all of this code is the tutorial and sample applications that can be found on the IronPython website. If you want to go further then this tutorial, or if you want to understand every line of code I suggest you start there.
The first thing to do is create our HelloWorld.py file in:
C:\IronPython\Projects\HelloWorld\
Now we have to import a whole bunch of stuff and create some helper functions. This code was taken from the IronPython tutorial file avalon.py, which should be installed in your C:\IronPython\Tutorial directory It had to change it slightly since it crated a System.Windows.Application object which has to be a singleton.
import clr clr.AddReferenceByPartialName("PresentationCore") clr.AddReferenceByPartialName("PresentationFramework") clr.AddReferenceByPartialName("WindowsBase") clr.AddReferenceByPartialName("IronPython") from math import * from System import * from System.Windows import * from System.Windows.Media import * from System.Windows.Media.Animation import * from System.Windows.Controls import * from System.Windows.Shapes import * from System.Threading import * from System.Windows.Threading import * import IronPython # Taken from the IronPython Samples def LoadXaml(filename): from System.IO import * from System.Windows.Markup import XamlReader f = FileStream(filename, FileMode.Open) try: element = XamlReader.Load(f) finally: f.Close() return element # Taken from the IronPython Samples def Walk(tree): yield tree if hasattr(tree, 'Children'): for child in tree.Children: for x in Walk(child): yield x elif hasattr(tree, 'Child'): for x in Walk(tree.Child): yield x elif hasattr(tree, 'Content'): for x in Walk(tree.Content): yield x
This code simply initializes IronPython, the Windows Presentation FrameWork, and creates two helper functions that will make the rest of our code easier.
The next thing that we do is create our main class which is of the type System.Windows.Application and initialize it:
class HelloWorld(Application): def __init__(self): Application.__init__(self) # Create the Window System.Windows.Window self.window = Window() # Load the Xaml self.window.Content = LoadXaml("hw.xaml") # Set the Window Title self.window.Title = "Hello World" # Make the Window the same size as the Xaml self.window.SizeToContent = SizeToContent.WidthAndHeight # Connect the buttons with their events self.connect_buttons() # Show the Window self.window.Show()
The first thing that we do is create our application’s window which is of type System.Windows.Window. Then we set the window’s Content to be our xaml code that we load.
Then we set the window’s SizeToContent property to size to the height and width of the content, which is the XAML that we loaded.
Then we call a helper function (not shown yet) which will connect our button’s Click event with a function, and then we call the Show() function to show the window.
The connect_buttons function is defined as follows:
def connect_buttons(self): """Connect the button to its clicked handler""" for item in Walk(self.window): if isinstance(item, Button): # It's our one and only button! # Connect the click event item.Click += self.on_button_clicked
What this function does is walk through all of our window’s children, and if it encounters a button it adds our on_button_clicked() function to that buttons Click handler.
on_button_clicked() is a pretty simple function that sets whatever button was clicked’s text to be “Hello World!”
def on_button_clicked(self, button, *args): # Set the Text to be Hello World! button.Content = "Hello World"
That’s basically it for the code, all we have to do now is make sure that something happens if this python file is launched directly:
if __name__ == "__main__": hw = HelloWorld() hw.Run()
Then if you want to launch this project and test it out you can start the Windows command line and browse to C:\ItronPython\Projects\HelloWord and launch the following command:
C:\IronPython\Projects\HelloWorld>C:\IronPython\ipy.exe HelloWorld.py
When you do you should be greeted with this:

Here is all the code in one nice window:
import clr clr.AddReferenceByPartialName("PresentationCore") clr.AddReferenceByPartialName("PresentationFramework") clr.AddReferenceByPartialName("WindowsBase") clr.AddReferenceByPartialName("IronPython") from math import * from System import * from System.Windows import * from System.Windows.Media import * from System.Windows.Media.Animation import * from System.Windows.Controls import * from System.Windows.Shapes import * from System.Threading import * from System.Windows.Threading import * import IronPython # Taken from the IronPython Samples def LoadXaml(filename): from System.IO import * from System.Windows.Markup import XamlReader f = FileStream(filename, FileMode.Open) try: element = XamlReader.Load(f) finally: f.Close() return element # Taken from the IronPython Samples def Walk(tree): yield tree if hasattr(tree, 'Children'): for child in tree.Children: for x in Walk(child): yield x elif hasattr(tree, 'Child'): for x in Walk(tree.Child): yield x elif hasattr(tree, 'Content'): for x in Walk(tree.Content): yield x class HelloWorld(Application): def __init__(self): Application.__init__(self) # Create the Window System.Windows.Window self.window = Window() # Load the Xaml self.window.Content = LoadXaml("hw.xaml") # Set the Window Title self.window.Title = "Hello World" # Make the Window the same size as the Xaml self.window.SizeToContent = SizeToContent.WidthAndHeight # Connect the buttons with their events self.connect_buttons() # Show the Window self.window.Show() def connect_buttons(self): """Connect the button to its clicked handler""" for item in Walk(self.window): if isinstance(item, Button): # It's our one and only button! # Connect the click event item.Click += self.on_button_clicked def on_button_clicked(self, button, *args): # Set the Text to be Hello World! button.Content = "Hello World" if __name__ == "__main__": hw = HelloWorld() hw.Run()




October 4th, 2006 at 5:28 am
[...] But… -> IronPython Hello World Tutorial [...]
October 23rd, 2006 at 8:41 am
Here’s a quick hat-tip for you, I mention your blog (this entry gets shown) in my recent 2-part video set that introduces new Python programmers to useful on-line resources:
http://showmedo.com/videos/video?name=pythonOzsvaldIntroToPyResources2&fromSeriesID=27
I’m looking forward to seeing more tutorials on here
Ian.

selsine Says:October 26th, 2006 at 8:20 am
Hey Ian,
That is too cool! Thank you very much for the tip of the hat! Keep up the good work on your site too!
January 1st, 2007 at 3:27 pm
[...] learning python » Blog Archive » IronPython Hello World Tutorial (tags: python) [...]
May 28th, 2007 at 8:05 am
Finding difficult in executing Iron Python codes using IronPythonConsole.exe

selsine Says:June 5th, 2007 at 9:29 am
Hi Rakshith,
What sort of problems are you having? If you explain what you are trying to do and the issues that you are encountering in more detail I may be able to help you with your problem.
November 26th, 2007 at 10:16 pm
[...] IronPython Hello World Tutorial (LearningPython.com) [...]
December 26th, 2007 at 1:27 am
windows vista tutorial
Nice points…