Sunday, February 7, 2010

WinForms test

One of my first challenges is to write some IronPython script to create a simple GUI using the WinForms library and get this to run on both my Vista VPC and Ubuntu VPC. After poking around I settled for the simple script below:


import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import Application, Form

class IForm(Form):

    def __init__(self):
        self.Text = 'Simple'
        self.Width = 250
        self.Height = 200
        self.CenterToScreen()

Application.Run(IForm())


I used the a version of Notepad called the Programmer's Notepad to enter this script and saved it as "WinFormsTest.py".


To run this script in Vista I opened a command window (cmd.exe) and executed:


ipy WinFormsTest.py



(note that I'd already CDed to the directory where WinFormsTest.py had been saved, and I'd also added the folder for IronPython 2.6 to the environmental variable called "path"). On execution the following simple window appeared in the middle of my screen:


So far so good. But what about running the same script on my Ubuntu VPC under mono? I created a text file containing the same script on my Ubuntu VPC and saved it in the same directory as I had unzipped IronPython 2.6. Then, in a terminal window I executed:

mono ipy.exe WinFormsTest.py

and low and behold the window below popped up:

Success!

So it seems a pretty straightforward matter to create WinForms with IronPython and run it in both Windows and Linux environments... that is very cool indeed! However, I'm used to building my GUIs in Visual Studio and so I wondered if I could somehow continue to do that, rather than build my forms up control by control at the code level - the topic of my next posting (if I can get it to work that is!).

By the way, if you don't want to use WinForms then GTK# is a perfectly decent alternative.

No comments:

Post a Comment