I was messing around with IronPython and WinForms between cleaning rooms of my apartment this weekend and came up with an example to demonstrate how easy it is to create a Windows Application, albeit a lame one. This however should serve as an example of 1) How well IronPython integrates with the .NET Framework and 2) How easy it is to code desktop applications using IronPython.
Is anyone interested in this? If so I can possibly whip up a small tutorial or if given an example app someone would like to see, some more code examples.
#file: winform.py
#auth: Tim Riley
#import statements
import sys
sys.path.append(r'c:\python24\lib')
import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
from System.Drawing import Point
from System.Windows.Forms import Application, Form, Button, Label
class HelloWorldForm(Form):
def __init__(self):
self.Text = "Hello World"
self.Name = "Hello World"
self.Height = 200
self.Width = 300
self.count = 0
self.label = Label()
self.label.Text = "Here is a label."
self.label.Location = Point(50, 50)
self.label.Height = 30
self.label.Width = 200
button = Button()
button.Text = "Click Here"
button.Location = Point(50, 100)
button.Click += self.buttonPressed
self.Controls.Add(self.label)
self.Controls.Add(button)
def buttonPressed(self, sender, args):
if self.count >= 20: self.count = 0
self.count += 1
self.label.Text = "You have clicked me %s times." % self.count
myform = HelloWorldForm()
Application.Run(myform)