Author Topic: IronPython Winforms Example  (Read 6478 times)

0 Members and 1 Guest are viewing this topic.

TR

  • Guest
IronPython Winforms Example
« on: August 28, 2006, 09:35:28 AM »
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.

Code: [Select]
#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)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: IronPython Winforms Example
« Reply #1 on: August 28, 2006, 10:36:07 AM »
Very cool Tim, thank you for taking the time to code and post it. Is there a web site that details how to install IronPython so it can be used with an IDE like SharpDevelop etc? Thanks bud (when you've time).

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

TR

  • Guest
Re: IronPython Winforms Example
« Reply #2 on: August 28, 2006, 11:21:50 AM »
If you go to the main IronPython page you will see a link that details how to setup IronPython with Visual Studio, but as for SharpDevelop I'm not sure.

For IronPython I have been using:
* JEdit
* Reflector for .NET
* Windows command line.

You can replace Jedit with any text editor that supports .py syntax highlighting and you should be fine.

If you add the path to ipy.exe to the windows path variable then all you need to do is type ipy myfile.py from the windows command line and it will execute your script for you. This us useful for the development phase but once you have an application ready for distribution you can issue the command switch –X:SaveAssemblies and it will generate an executable file. example: ipy –X:SaveAssemblies myfile.py will generate myfile.exe which can then be distributed to any computer running .NET 2.0 or a unix machine running the latest version of Mono.