Getting Started with IronPython and Silverlight

Examples of Minimal IronPython Applications

Some of the Silverlight controls

Follow my exploration of living a spiritual life and finding the kingdom at Unpolished Musings.

Minimal IronPython Example 1

This is just about the shortest IronPython file for the in your dynamic application:

from System.Windows import Application
from System.Windows.Controls import Canvas

xaml = Application.Current.LoadRootVisual(Canvas(), "app.xaml")
xaml.textblock.Text = 'Hello world from IronPython'

Technically it's cheating of course, it's loading a xaml file from the 'xap'. The call to Application.Current.LoadRootVisual loads the xaml and makes it visible in the control. It also returns the object tree created, which we can manipulate programatically. Because the xaml (below) includes a textblock which has an x:Name attribute set, we can access the textbox as an attribute of the object tree.

<Canvas x:Class="System.Windows.Controls.Canvas"

  <TextBlock x:Name="textblock" FontSize="30">Hello world from XAML</TextBlock>


XAML defines a tree of objects that represent the user interface. You can use this to create elements of the interface include animations and 'skinning' (styling) UI elements.

Anything that can be done with XAML can be done from code and there is a direct relationship between XAML elements and their corresponding classes. For example, TextBlock XAML elements have a corresponding TextBlock class. Although I am generally no fan of visual design tools, nor of writing XML by hand, there are occasions when XAML is less verbose than the equivalent code. Dynamic languages are particularly good at manipulating text, and XAML is just text, so you can dynamically generate and consume XAML.

Accessing named elements in the object tree loaded from XAML as attributes is another nice consequence of using a dynamic language.

Minimal IronPython Example 2

This example uses no XAML at all:

from System.Windows import Application
from System.Windows.Controls import Canvas, TextBlock

canvas = Canvas()
textblock = TextBlock()
textblock.FontSize = 24
textblock.Text = 'This Really Works!!'

Application.Current.RootVisual = canvas

Instead of loading XAML we directly assign to the RootVisual.

IronPython Controls Example 1

This example shows off some of the shiny new Silverlight controls. The UI is created from XAML.

I won't list all the XAML here, you can easily download it and take a peek, but here is a section of it:

<TextBlock Text="TextBlock" Margin="5"/>
<TextBox Text="TextBox" Margin="5" />
<WatermarkedTextBox x:Name="watermark" Margin="5"/>
<ContentControl Content="ContentControl" Margin="5"/>
<Button Content="Button w/ ToolTip" Margin="5" ToolTip="ToolTip"/>
<ToggleButton Content="ToggleButton" Margin="5"/>
<RadioButton Content="RadioButton" Margin="5"/>
<CheckBox Content="CheckBox" IsThreeState="True" Margin="5" />
<HyperlinkButton Content="HyperlinkButton"
 NavigateUri="" Margin="5"/>
<RepeatButton Content="RepeatButton" Margin="5"/>

Some of these controls are part of the 'extended controls' that come with the Silverlight SDK for Visual Studio. Using them requires the addition of a couple of assemblies to the application, and therefore also to the manifest file:

<AssemblyPart Source="System.Windows.Controls.dll" />
<AssemblyPart Source="System.Windows.Controls.Data.dll" />

These extend the System.Windows.Controls namespace with new controls. There are even more controls provided by Microsoft in the Silverlight Toolkit project on Codeplex (with source code and unit tests). These include charting controls.

IronPython Controls Example 2

Using controls from code

Of course using the new controls from code is much more interesting. This example creates a button and a text box. When the button is pushed (or the user hits enter), text in the text box is put into a text block above it. The controls are laid out using a couple of StackPanel controls. The StackPanel is one of the basic container controls available in Silverlight.

This code shows how easy to use from code the controls are.

from System.Windows import Application, Thickness
from System.Windows.Controls import (
    Button, Orientation, TextBlock,
    StackPanel, TextBox
from System.Windows.Input import Key

root = StackPanel()
textblock = TextBlock()
textblock.Margin = Thickness(20)
textblock.FontSize = 18
textblock.Text = 'Stuff goes here'

panel = StackPanel()
panel.Margin = Thickness(20)
panel.Orientation = Orientation.Horizontal

button = Button()
button.Content = 'Push Me'
button.FontSize = 18
button.Margin = Thickness(10)

textbox = TextBox()
textbox.Text = "Type stuff here..."
textbox.FontSize = 18
textbox.Margin = Thickness(10)
textbox.Width = 200
#textbox.Watermark = 'Type Something Here'

def onClick(s, e):
    textblock.Text = textbox.Text
    textbox.Text = ""

def onKeyDown(sender, e):
    if e.Key == Key.Enter:
        e.Handled = True
        onClick(None, None)

button.Click += onClick
textbox.KeyDown += onKeyDown


Application.Current.RootVisual = root

Simple Animation Example

One of the great things you can do with Silverlight is create animations and visual effects. Creating simple animations can be a bit fiddly (several different classes involved), but the basics are straightforward.

This example animates the font size of some text when the mouse moves over it.

from System import TimeSpan
from System.Windows import Application, Duration, PropertyPath
from System.Windows.Controls import Canvas, TextBlock
from System.Windows.Media.Animation import (
    DoubleAnimation, Storyboard

root = Canvas()

Application.Current.RootVisual = root


t = TextBlock()
t.FontSize = 20
t.Text = 'Move the Mouse Over Me'

sb = Storyboard()
duration = Duration(TimeSpan.FromSeconds(0.25))
a = DoubleAnimation()
a.Duration = duration
sb.Duration = duration
sb.AutoReverse = True

Storyboard.SetTarget(a, t)
Storyboard.SetTargetProperty(a, PropertyPath('FontSize'))
a.From = 20
a.To = 30

def anim(s, e):
    print 'Starting'

t.MouseEnter += anim

root.Resources.Add('StoryBoard', sb)

I particularly like the little dance we have to go through to set the duration. Being able to set a float directly would be waaaay too easy...

The next article explores some of the Silverlight APIs, using a web based ide to experiment:

For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store.

Hosted by Webfaction

Return to Top

Page rendered with rest2web the Site Builder

Last edited Sat Aug 14 14:34:38 2010.