PostHeaderIcon Iron Python and Silverlight Tutorial

iron-python-and-silverlight-tutorial

I’ve been playing with Microsoft Silverlight and would like to share how to use it. First what is Microsoft Silverlight?

Microsoft Silverlight is a programmable web browser plugin that enables features such as animation, vector graphics and audio-video playback that characterizes rich Internet applications

Currently in Version 2 and Version 3 is in Beta. Version 2 brought with it the use of the Dynamic Language Runtime environment or DLR this allows for languages using .net to implement Silverlight Applications Such as C# VB.net Iron Python and Iron Ruby. To explain it better you can now use Python in your browser!

Silverlight uses XAML an XML based language developed by microsoft for use with silverlight

Extensible Application Markup Language, or XAML (pronounced “zammel”), is an XML-based markup language developed by Microsoft

Alright now that we know what Silverlight is we can start to use it. The examples I will be developing will be for IronPython 2.01

Our Import Statements.

from System.Windows import *
from System.Windows.Controls import *

System.Windows.Controls is our Controls for Buttons Text boxes etc

We than create our box, basically a StackPanel allows us to stack elements of the GUI this initial StackPanel creates a Vertical Box

main = StackPanel()

Now We create our StackPanel which orients horizontal which we add a textBox and a Button

layer1 = StackPanel()
layer1.Orientation = Orientation.Horizontal
textb = TextBox()
textb.Width = 100
button = Button()
button.Content = “Generate String“

Next we create our stringCheese Function alls it does is print a string to our textbox

def stringCheese(s, e):
textb.Text = “”
textb.Text = “Hello from IronPython!”

assign the button to the function

button.Click += stringCheese

>

now add the elements

layer1.Children.Add(textb)
layer1.Children.Add(button)
main.Children.Add(layer1)

This last line loads the code

Application.Current.RootVisual = main

Now save this file as app.py in a folder called app_folder and move to your DOS Console type:

\Silverlight\bin\Chiron.exe /d:C:\app_folder\ /z:C:\app.xap

this will generate the app.xap file your python needs to be named app.py

In order to view the page in a browser simply add the html tags as described here

And that about sums it up. The Controls such as buttons text boxes are really just the Python implementation of using Xaml code. Although you can use Xaml code in your python apps I prefer pure python way instead cause it’s Cheesy….

Have fun!

2 Responses to “Iron Python and Silverlight Tutorial”

Leave a Reply

Search
Awesome Links
Subscribe
Vote for Flyninja
Random Reading