IronPython Tutorial: Advanced (Events, Delegates, WinForms, WPF)
Материал из AvbWiki
Содержание |
Advanced IronPython
Events and Delegates
File System Watcher
def handle(*args): print args from System.IO import * w = FileSystemWatcher() w.Path = "." w.Changed += handle w.Created += handle w.Deleted += handle w.EnableRaisingEvents = True w.Changed -= handle w.Created -= handle w.Deleted -= handle
Improved Event Handler
def handle(w, a): print a.ChangeType, a.FullPath from System.IO import * w = FileSystemWatcher() w.Path = "." dir(FileSystemEventArgs) # FileSystemEventArgs - the information about the event raised w.Changed += handle w.Created += handle w.Deleted += handle w.EnableRaisingEvents = True w.Changed -= handle w.Created -= handle w.Deleted -= handle
Defining events in Python
- pyevent - a module providing Python event support
- make_event - a function that returns an event tuple containing both the callable object and the object that allows connecting and unconnecting to the event.
import pyevent # hook - allows a user to hook a function up to the event # caller - allows the owner of the event to cause the event to be raised hook,caller = pyevent.make_event()
- Create a class with event on new instance
import pyevent class MyClass(object): OnNewInstance,_NewInstance= pyevent.make_event() def __new__(cls): res = object.__new__(object) MyClass._NewInstance(res) # Event caller def NewInst(x): print 'new inst: ', x MyClass.OnNewInstance += NewInst # Event hook (i.e. handler) a = MyClass() MyClass.OnNewInstance -= NewInst # Remove event handler
Windows Forms on Python
- winforms Python module executes some Windows Forms initialization code as part of the import statement.
import winforms from System.Windows.Forms import * from System.Drawing import * def click(*args): print args f = Form() f.Show() f.Text = "My First Interactive Application" f.Click += click # Make some tests f.Click -= click dir(MouseEventArgs)
- Improve click event
import winforms from System.Windows.Forms import * from System.Drawing import * def click(f, a): l = Label(Text = "Hello") l.Location = a.Location f.Controls.Add(l) f.Click += click # Make some tests for i in f.Controls: i.Font = Font("Verdana", 15) for i in f.Controls: i.Text = "Hi" # Make some tests f.Controls.Clear()
- The some application in one Python module:
import clr clr.AddReferenceByPartialName("System.Windows.Forms") clr.AddReferenceByPartialName("System.Drawing") from System.Windows.Forms import * from System.Drawing import * f = Form() font = Font("Verdana", 15) f.Text = "My First Interactive Application" def click(f, a): l = Label(Text = "Hello") l.AutoSize = True l.Location = a.Location l.Font = font f.Controls.Add(l) f.Click += click Application.Run(f)
Windows Presentation Foundation (Avalon) on Python
- Windows Presentation Foundation Simple Demo
from avalon import * w = Window() w.Show() w.Title = "My Avalon Application" w.SizeToContent = SizeToContent.WidthAndHeight # Make some tests w.Content = TextBlock() w.Content.Text = "Hello IronPython!" w.Content.FontSize = 50 w.SizeToContent = SizeToContent.WidthAndHeight # Make some tests w.Content = None
- WPF Calculator
from avalon import * w = Window() w.Show() w.Content = LoadXaml("calc.xaml") # Make some tests def Walk(tree): yield tree if hasattr(tree, 'Children'): for child in tree.Children: for x in Walk(child): yield x elif hasattr(tree, 'Child'): for x in Walk(tree.Child): yield x elif hasattr(tree, 'Content'): for x in Walk(tree.Content): yield x for n in Walk(w): print n # Make some tests [ n for n in Walk(w) if isinstance(n, Button) ] # To save the button list object in a variable, use the interpreter's "_" variable, # which always holds the last non-None value result printed by the console: buttons = _ # Make some tests for b in buttons: b.FontSize *= 2 for b in buttons: b.Foreground = SolidColorBrush(Colors.Blue)
- Calculator's XAML from IronPython tutorial
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/sparkle"> <Canvas Name="Layer2"> <Rectangle Stroke="#FF000000" RadiusX="0" RadiusY="0" StrokeThickness="5" StrokeMiterLimit="2" StrokeEndLineCap="Flat" StrokeStartLineCap="Flat" StrokeLineJoin="Miter" Name="Rectangle1" Width="231" Height="311" Canvas.Left="-1" Canvas.Top="0"> <Rectangle.Fill> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5"> <LinearGradientBrush.GradientStops> <GradientStop Color="sc#1.000000, 0.157502, 0.408674, 0.808030" Offset="0"/> <GradientStop Color="#FFFFFFFF" Offset="1"/> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Rectangle.Fill> <Rectangle.RenderTransform> <TransformGroup> <TranslateTransform X="-115.5" Y="-155.5"/> <ScaleTransform ScaleX="1" ScaleY="1"/> <SkewTransform AngleX="0" AngleY="0"/> <RotateTransform Angle="0"/> <TranslateTransform X="115.5" Y="155.5"/> <TranslateTransform X="0" Y="0"/> </TransformGroup> </Rectangle.RenderTransform> </Rectangle> </Canvas> <Canvas Name="Layer1" > <TextBox Name="Result" Width="135" Height="33.5533333333333" Canvas.Left="20" Canvas.Top="65"/> <RichTextBox Name="Title" IsReadOnly="True" Width="191" Height="48" Canvas.Left="18" Canvas.Top="11" BorderBrush="sc#0.000000, 0.510939, 0.510939, 0.510930" Background="sc#0.000000, 1.000000, 1.000000, 1.000000" Foreground="#FF000000"> <RichTextBox.RenderTransform> <TransformGroup> <TranslateTransform X="-95.499999999999872" Y="-24"/> <ScaleTransform ScaleX="1" ScaleY="1"/> <SkewTransform AngleX="0" AngleY="0"/> <RotateTransform Angle="0"/> <TranslateTransform X="95.499999999999872" Y="24"/> <TranslateTransform X="0" Y="0"/> </TransformGroup> </RichTextBox.RenderTransform> <FlowDocument> <Paragraph> <TextBlock FontFamily="Perpetua Titling MT" FontStyle="Italic" FontWeight="700" FontSize="20" Foreground="sc#1.000000, 0.172911, 0.172911, 0.172910">IronCalc</TextBlock> <TextBlock FontFamily="Perpetua Titling MT" FontStyle="Italic" FontWeight="700" FontSize="20" Foreground="sc#1.000000, 0.172911, 0.172911, 0.172910">2007</TextBlock> </Paragraph> </FlowDocument> </RichTextBox> <Button Name="One" Content="1" Width="40" Height="40" Canvas.Left="20" Canvas.Top="110"/> <Button Name="Nine" Content="9" Width="40" Height="40" Canvas.Left="120" Canvas.Top="210"/> <Button Name="Eight" Content="8" Width="40" Height="40" Canvas.Left="70" Canvas.Top="210"/> <Button Name="Five" Content="5" Width="40" Height="40" Canvas.Left="70" Canvas.Top="162"/> <Button Name="Four" Content="4" Width="40" Height="40" Canvas.Left="20" Canvas.Top="162"/> <Button Name="Two" Content="2" Width="40" Height="40" Canvas.Left="70" Canvas.Top="110"/> <Button Name="Three" Content="3" Width="40" Height="40" Canvas.Left="120" Canvas.Top="110"/> <Button Name="Six" Content="6" Width="40" Height="40" Canvas.Left="120" Canvas.Top="162"/> <Button Name="Multiply" Content="*" Width="40" Height="40" Canvas.Left="170" Canvas.Top="162"/> <Button Name="Seven" Content="7" Width="40" Height="40" Canvas.Left="20" Canvas.Top="210"/> <Button Name="Subtract" Content="-" Width="40" Height="40" Canvas.Left="170" Canvas.Top="210"/> <Button Name="Zero" Content="0" Width="40" Height="40" Canvas.Left="20" Canvas.Top="260"/> <Button Name="DecimalPoint" Content="." Width="40" Height="40" Canvas.Left="70" Canvas.Top="260"/> <Button Name="Equals" Content="=" Width="40" Height="40" Canvas.Left="120" Canvas.Top="260"/> <Button Name="Plus" Content="+" Width="40" Height="40" Canvas.Left="170" Canvas.Top="260"/> <Button Name="Divide" Content="/" Width="40" Height="40" Canvas.Left="170" Canvas.Top="110"/> <Button Name="Clear" Content="C" Width="40" Height="40" Canvas.Left="169" Canvas.Top="62"/> </Canvas> </Canvas>

