Ironpython WPF
Ironpython WPF
Douglas Blank
Bryn Mawr College
Programming Paradigms
Spring 2010
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestApp" Width="640" Height="480">
<StackPanel>
<Label>Iron Python and WPF</Label>
<ListBox Grid.Column="0" x:Name="listbox1" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=title}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
IronPython + XAML
import sys
if 'win' in sys.platform:
import pythoncom
pythoncom.CoInitialize()
import clr
clr.AddReference("System.Xml")
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
from System.IO import StringReader
from System.Xml import XmlReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application
Application().Run(win)
IronPython with XAML Files
import clr
from Entry import Entry
clr.AddReferenceToFileAndPath("lib/WpfExtension.dll")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("WindowsBase")
clr.AddReferenceByPartialName("IronPython")
clr.AddReferenceByPartialName("Microsoft.Scripting")
from System.IO import File
from System.Windows.Markup import XamlReader
from System.Windows import Application
def listbox1_OnSelectionChanged(self, event):
for item in event.AddedItems:
print item.title
titles = [Entry("Book", "Great For reading"),
Entry("Shelf", "Ideal for storing stuff"),
Entry("Cupboard", "Store and hide stuff")]
file = File.OpenRead('Main.xaml')
window = XamlReader.Load(file)
window.listbox1.SelectionChanged += listbox1_OnSelectionChanged
window.listbox1.ItemsSource = titles
Application().Run(window)
C# Code Entry Object
using System;
using System.Windows;
using Microsoft.Scripting.Runtime;
[assembly: ExtensionType(
typeof(FrameworkElement),
typeof(Sample.Scripting.FrameworkElementExtension))]
namespace Sample.Scripting {
public static class FrameworkElementExtension {
[System.Runtime.CompilerServices.SpecialName]
public static object GetBoundMember(FrameworkElement e, string n) {
object result = e.FindName(n);
if (result == null) {
return OperationFailed.Value;
}
return result;
}
}
}
import clr
clr.AddReference("System.Speech”)
from System.Speech.Recognition import *
count = 0
def main():
sre = SpeechRecognitionEngine()
sre.SetInputToDefaultAudioDevice()
sre.UnloadAllGrammars()
gb = GrammarBuilder()
gb.Append(Choices('cut', 'copy', 'paste', 'delete'))
sre.LoadGrammar(Grammar(gb))
if __name__ == '__main__':
import sys
if sys.platform == 'cli':
from System.Threading import Thread, ThreadStart
thread = Thread(ThreadStart(main))
thread.Start()
else:
main()
xaml_str=""" XAML Shapes
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="A Crazy Window" Height="300" Width="300"
WindowStyle="None" AllowsTransparency="True"
Background="{x:Null}"
>
<Grid Name="gid">
<Viewbox Stretch="Uniform">
<Path Fill="#80D0E0FF" Stroke="Red" StrokeThickness="3"
HorizontalAlignment="Center" VerticalAlignment="Center"
Data="M79,3L65,82 17,91 50,138 96,157 104,192 175,154 190,167 218,78
156,76 157,9 111,39z"/>
</Viewbox>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}}, Path=Title}"
FontSize="18" Background="Transparent" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="20"
FontFamily="Impact" Foreground="#C030A060"/>
<TextBlock Text="Right Click Anywhere to Close" Background="Black" Width="200"
Foreground="White"
VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14"
TextWrapping="Wrap" />
</Grid>
</Window>
"""