IronPython and Silverlight Tutorial

DON'T post new tutorials here! Please use the "Pending Submissions" board so the staff can review them first.
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

IronPython and Silverlight Tutorial

Post by maboroshi »

IronPython and Silverlight

Ive 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 3. 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.

Code: Select all

from System.Windows import *
from System.Windows.Controls import *
System.Windows.Controls is our Controls so 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

Code: Select all

main = StackPanel()
Now We create our StackPanel which orients horizontal which we add a textBox and a Button

Code: Select all

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

Code: Select all

def stringCheese(s, e):
    textb.Text = ""
    textb.Text = "Hello from IronPython!"
assign the button to the function

Code: Select all

button.Click += stringCheese
now add the elements

Code: Select all

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

main.Children.Add(layer1) 

This last line loads the code

Code: Select all

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

Code: Select all

<Path to IronPython>\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 simple add the html tags as describe here

Code: Select all

http://msdn.microsoft.com/en-us/library/cc189089(VS.95).aspx
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 :D

Any way have fun

*cheers

Maboroshi
Last edited by maboroshi on 12 Jan 2010, 12:51, edited 2 times in total.

User avatar
computathug
Administrator
Administrator
Posts: 2693
Joined: 29 Mar 2007, 16:00
17
Location: UK
Contact:

Post by computathug »

Nice work buddy!!

I might give this a try with ruby :wink:
The devil can cite Scripture for his purpose.
-- William Shakespeare, "The Merchant of Venice"
https://tshirt-memes.com

User avatar
ph0bYx
Staff Member
Staff Member
Posts: 2039
Joined: 22 Sep 2008, 16:00
15
Contact:

Post by ph0bYx »

Moved to Development/Coding.

Post Reply