This article shows you how to build a simple IronPython application by using .NET components. The significance of being able to build a Pythonic application while using the .NET Framework for the interface and other potential functionality is that it makes your application much less complex and easier to maintain.
Let’s be honest; a .NET application is often a lot of work and it takes much more code to accomplish most programming tasks than it does with Python. Yet .NET has a lot more methods and a more powerful OO library for developing industrial applications than Python does. What if you could get .NET’s industrial component functionality along with Python’s declarative scripting capability to implement a Python-based application?
Welcome to IronPython, a library that allows you to leverage the .NET Framework using Python syntax and coding styles. IronPython has all the original Python functions and modules, but also allows you to add (or integrate) .NET component libraries as well.
Even Microsoft has contributed to IronPython by creating a tools package called Python Tools, which enables you to create IronPython projects using Visual Studio. I won’t get into Python Tools; instead, I’ll start with a simple example using the IronPython console to demonstrate the concepts of building IronPython applications.
If you ever used Python with other frameworks such as Nokia’s QT, the concept of interfacing Python with more powerful frameworks should be nothing new. And if you are a .NET programmer who has never used Python, learning IronPython is a great way to get started with Python in general. Before I started using IronPython, I knew both .NET and Python. Being able to leverage both within one application environment was great!
Creating Simple IronPython Applications Using the IronPython Console
Before getting started, you have to download the IronPython runtime from Codeplex at https://ironpython.codeplex.com/. After downloading, open the IronPython console and get ready to type some code.
The first thing to know is how to add the standard .NET library reference to an IronPython program. It is very easy; simply do it with the code below:
This line of code integrates all the standard .NET libraries, making them available to the IronPython runtime libraries. You have significantly increased the potential functionality of your IronPython application by leaps and bounds.
Don’t believe me? Take a look at these lines of code:
str = 'Hello World' str.ToUpper()
You might say, “Wait; the Python string function does not have a method called ToUpper.” Well, it does now. In fact, it has a bunch of new string methods that it gets from the .NET library component that defines string methods (part of the standard .NET library). After you paste or type the code above into the console, you see that it actually works and converts the string to ‘Hello World’.
Now add a reference from the .NET Windows libraries for handling a Windows Form:
Type or paste this code into the console; you should get no errors. Add some Python code that imports only the methods you want to use from the system.windows.forms library and create a simple Windows Form with a single Button that is labeled ‘Hello World’:
from System.Windows.Forms import Application, Button, Form # import only what we need form = Form() # create a new Form object instance form.Text = "Hello World" # Add the form's Title text button = Button(Text="Button Text") # Add a simple button Label form.Controls.Add(button) # Add button control to form's control list
To run the application, add the line of code below:
After running the form code, you should see a simple Windows Form appear with a button on it labeled ‘Button Text’ that does nothing when you click it. By doing just this bit of code, you have successfully created your first simple IronPython application.
To expand this example, add a button click function to make the button print a message to the console:
def click(sender, event): print "Hello World!"
Now wire up the button to the event method and run the application:
button.Click += click Application.Run(form)
That was easy. Here is the full code for this example:
import clr clr.AddReference('System.Windows.Forms') clr.AddReference('System.Windows.Media.Drawing') from System.Windows.Forms import Application, Button, Form form = Form() form.Text = "Hello World" button = Button(Text="Button Text") form.Controls.Add(button) def click(sender, event): print "Hello World!" button.Click += click Application.Run(form)
When you build IronPython applications, it might not always be obvious that an assembly contains the import reference you are looking for.
If you are unsure of the interfaces that are part of a .NET assembly you want to import, use the code below to find out what is in the assembly (this example is for System.Collections):
import System.Collections interfaces = [entry for entry in dir(System.Collections) if entry.startswith('I')] for entry in interfaces: print entry