IronPython revisited (99 bottlse of beer)

vegaseat 3 Tallied Votes 554 Views Share

If you use the Windows OS, then IronPython allows you to create a GUI program taking advantage of the .NET framework. You also have the option to compile the program into a standalone .exe file (and the needed .dll file).

''' ip_ListBox_99BoB.py
add '99 bottles of beer' lyrics to a .NET list box
simplified code using some defaults

IronPython gives access to the Windows .NET or Linux Mono libraries
download the IronPython installer version 2.7.3 from
http://ironpython.codeplex.com/releases/view/81726

run with the IronPython interpreter this way ...
(might have to add full path)
ipy.exe ip_ListBox_99BoB.py

tested with IronPython 2.7.3 by  vegaseat  28may2013
'''

import clr

clr.AddReference('System.Windows.Forms')
clr.AddReference("System.Drawing")

import System
from System.Windows.Forms import *
from System.Drawing import *
from System.Collections import ArrayList

class BeerSong(Form):
    def __init__(self):
        # width, height of form
        self.ClientSize = System.Drawing.Size(220, 326)
        self.Text = '99 BoB lyrics'
        # create the listbox and fill the form (self) with it
        box = ListBox()
        # light yellow
        box.BackColor = System.Drawing.Color.FromArgb(255, 255, 224)
        box.Dock = DockStyle.Fill
        self.Controls.Add(box)

        # move the lyrics into ArrayList then transfer to the box
        array = ArrayList()
        bottle = "%s bottle"
        beer = "s of beer on the wall!"
        take = "Take one down, pass it around,"
        for k in range(99, 0, -1):
            # an exercise in slicing
            s1 = bottle % k + beer[k==1:]
            s2 = (bottle % k + beer[k==1:])[:-13] + "!"
            s3 = bottle % (k-1 or "No")
            s4 = beer[k==2:-1] + "!"
            array.Add(s1)
            array.Add(s2)
            array.Add(take)
            array.Add(s3+s4)
            array.Add(" ")
        box.DataSource = array


Application.Run(BeerSong())
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The above IronPython script can be compiled to a standalone .exe file this way ...

''' compile_ip1.py
compile an ironpython sript file to an executable file
creates a Windows .exe file and an associated .dll file
in this case ...
ip_ListBox_99BoB.exe  (3kb)
ip_ListBox_99BoB.dll  (13kb)

it is best to put this file into a special directory
together with the ironpython script file you want to compile

this code needs to be run with IronPython 2.7.3
'''

import subprocess

# the ironpython script file you want to compile ...
ip_scriptfile = "ip_ListBox_99BoB.py"

# location of ironpython and compile utility
ipython = "C:/IronPython2.7.3/ipy.exe"
utility = "C:/IronPython2.7.3/Tools/Scripts/pyc.py",
main = "/main:" + ip_scriptfile
target = "/target:winexe"
subprocess.call([ipython, utility, main, target])
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I have read that Microsoft has withdrawn support for IronPython. I guess they have grown worried about the popularity of Python (not one of the languages under their control).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.