Hello World with IronPython
IronPython & Windows Forms, Part II

Note
This is part of a series of tutorials on using IronPython with Windows Forms.
Contents
Introduction
Getting started with IronPython is really easy. In these blog entries I'll show you how to create a simple application. We'll add widgets to a form, create callback handlers, and handle events. As we go, we'll also cover more widgets (like menus and toolbars), simple threading. We may even get as far as compiling C-sharp code and accessing it from IronPython, and embedding the IronPython engine into C-sharp projects.
This entry will demonstrate creating the simplest possible application.
First of all you'll need to download the latest version of IronPython, you will also need Python 2.4 installed on your machine.
As IronPython is a .NET framework application, you will also need the .NET Framework Version 2.0 Redistributable Package.
Note
Running these examples require us to hand control flow of the program to the .NET event loop. This means that you can't run them from the IronPythonConsole.
See the IronPython tutorial (included in the download) for how to use Windows Forms from the interactive interpreter.
You should save these examples as files, and run them (from the command line) using :
ipy.exe exampleApp.py
It can be convenient to create a simple batch file (.bat) which will run your program when you double click on it. This is just a text file, with the '.bat' extension, which contains the command line instruction as above.
Starting to Code
The very first thing our program needs to do is add the Python standard library to the path [1]. This means you can use standard library modules (like os) in the usual way.
sys.path.append(r'C:\Python24\Lib')
Next we need to add references to the CLR assemblies we will use. This allows us to import them. For the first example, we only use the System.Windows.Forms assembly.
clr.AddReference("System.Windows.Forms")
We then import the names we want to use :
Most GUIs will have a main form, which will actually use (or be a subclass of) the Form class.
def __init__(self):
self.Text = 'Hello World'
self.Name = 'Hello World'
The Text attribute we have set is actually a property of the Form class. It sets the title bar text. .NET makes heavy use of properties. You can see a list of all the properties (and methods) available on the Form class, on the Form Members documentation page.
All GUI widgets are 'controls' in Microsoft terminology. We set a Name, so that later we can find this control by its name. This is mainly useful for testing.
To run this simple application, we need to create an instance of our HelloWorldForm class and hand it to the event loop. We do this with the following code :
Application.Run(form)
The Application Class :
Provides static methods and properties to manage an application, such as methods to start and stop an application...
starts an application message loop on the current thread and, optionally, makes a form visible.
Easy hey.
The First Example in Full
So the full code for our first simple application is :
sys.path.append(r'C:\Python24\Lib')
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Application, Form
class HelloWorldForm(Form):
def __init__(self):
self.Text = 'Hello World'
self.Name = 'Hello World'
form = HelloWorldForm()
Application.Run(form)
If you run this program, you will see something like :

This creates a very plain, resizable window. It has minimize, maximize and close buttons. It has the title which we gave it, "Hello World".
The screenshot below (thanks to Seo Sanghyeon), is the same program running on Mono [3] :

The Form class by default exits the application (calls Application.Exit()) when we click on the close button. Because we haven't overridden this behaviour, you will see that this happens.
So now you know enough to go and write a killer application... well possibly not quite.
| [1] | To run your applications on machines without Python installed, you can distribute the standard library with your program. |
| [2] | static meaning you call it on the Application class rather than on an instance. |
| [3] | Seo informs me that it is Mono, running on Debian GNU/Linux, unstable branch with GNOME and the GNOME theme "Glider". The window manager he uses is Metacity. |
For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store.
Last edited Fri Nov 27 18:32:35 2009.
Counter...

