BLOG: Nine Works ==> 動的なアニメーション作成
http://nine-works.blog.ocn.ne.jp/blog/2011/01/post_8f38.html
を参考にさせていただきました。
C#のプログラムをIronPythonに変換しました。
そして、少しアレンジしました。
C#とIronPyhonを比べてみてください。 ( Dynamic Animation )
http://nine-works.blog.ocn.ne.jp/blog/2011/01/post_8f38.html
を参考にさせていただきました。
C#のプログラムをIronPythonに変換しました。
そして、少しアレンジしました。
C#とIronPyhonを比べてみてください。 ( Dynamic Animation )
#
# 動的なアニメーション作成.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System.Windows.Markup import XamlReader
from System import Object, TimeSpan
from System.Windows import ( Window, Application, NameScope, Thickness, Duration,
PropertyPath, HorizontalAlignment, VerticalAlignment )
from System.Windows.Controls import StackPanel, Button, TextBlock
from System.Windows.Media import Color, Colors, SolidColorBrush
from System.Windows.Media.Animation import Storyboard, RepeatBehavior, DoubleAnimation, ColorAnimation
xaml_str="""
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="動的なアニメーション作成" Height="200" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="10"
Grid.Row="0" Grid.Column="0" Margin="5"
ToolTip="ボタン・アニメーションの種類と動作を指定します。" >
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<RadioButton x:Name="radioBig" IsChecked="True" Content="大きくなれ" Margin="10,5"/>
<RadioButton x:Name="radioSmall" Content="小さくなれ" Margin="10,5" />
<CheckBox x:Name="stopAnimationCheckBox" Content="アニメーション停止"
HorizontalAlignment="Center" Margin="10,5" />
</StackPanel>
</Border>
<Button Name="button1" Grid.Column="0" Grid.Row="1"
Width="90" Height="20" Content="おっきくなるよ"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Window>
"""
class ExWindow(Object):
def __init__(self):
win = XamlReader.Parse(xaml_str)
self.Root = win
self.setAttr("button1",win)
self.setAttr("stopAnimationCheckBox",win)
self.setAttr("radioBig",win)
self.setAttr("radioSmall",win)
self.radioBig.Checked += self.radioBig_Checked
self.radioSmall.Checked += self.radioSmall_Checked
self.EXPAND = ( 50.0, ) # タップル 変更不可能
#// アニメーションを行うコントロールをNameScopeに登録
#NameScope.SetNameScope(win, NameScope())
#win.RegisterName(self.button1.Name, self.button1)
#すでに、登録されているから...
self.CreateExpandAnimation()
def setAttr(self, name, xaml):
x = xaml.FindName(name)
if x != None : setattr(self, name, x)
else: print "NameError: '%s' object has no Name '%s'" %(type(xaml),name)
def CreateExpandAnimation(this):
#// ボタンの幅を拡大するアニメーション
#// Byは動的に値を設定するためClickイベントで設定
this._expandWidthAnimation = DoubleAnimation()
this._expandWidthAnimation.Duration = Duration(TimeSpan.FromMilliseconds(500))
#// ボタンの高さを拡大するアニメーション
#// Byは動的に値を設定するためClickイベントで設定
this._expandHeightAnimation = DoubleAnimation()
this._expandHeightAnimation.Duration = Duration(TimeSpan.FromMilliseconds(500))
#// ストーリーボードを作成
this._expandStoryboard = Storyboard()
this._expandStoryboard.Children.Add(this._expandWidthAnimation)
Storyboard.SetTargetName(this._expandWidthAnimation, this.button1.Name)
Storyboard.SetTargetProperty(
this._expandWidthAnimation,
PropertyPath(Button.WidthProperty))
this._expandStoryboard.Children.Add(this._expandHeightAnimation)
Storyboard.SetTargetName(this._expandHeightAnimation, this.button1.Name)
Storyboard.SetTargetProperty(
this._expandHeightAnimation,
PropertyPath(Button.HeightProperty))
#// イベントハンドラ追加
this.button1.Click += this.button1_Click
def button1_Click(self, sender, e):
if (self.stopAnimationCheckBox.IsChecked == False):
if (self.radioBig.IsChecked == True): by = self.EXPAND[0]
else:
if ( self.button1.Height <= self.EXPAND[0] ) : by = 0.0
else: by = -self.EXPAND[0]
if by != 0.0 :
self._expandWidthAnimation.By = by
self._expandHeightAnimation.By = by
self._expandStoryboard.Begin(self.Root)
def radioBig_Checked(self, sender, e):
self.button1.Content = "大きくなるよ"
def radioSmall_Checked(self, sender, e):
self.button1.Content = "小さくなるよ"
if __name__ == "__main__":
win = ExWindow()
Application().Run(win.Root)
# 動的なアニメーション作成.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System.Windows.Markup import XamlReader
from System import Object, TimeSpan
from System.Windows import ( Window, Application, NameScope, Thickness, Duration,
PropertyPath, HorizontalAlignment, VerticalAlignment )
from System.Windows.Controls import StackPanel, Button, TextBlock
from System.Windows.Media import Color, Colors, SolidColorBrush
from System.Windows.Media.Animation import Storyboard, RepeatBehavior, DoubleAnimation, ColorAnimation
xaml_str="""
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="動的なアニメーション作成" Height="200" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="10"
Grid.Row="0" Grid.Column="0" Margin="5"
ToolTip="ボタン・アニメーションの種類と動作を指定します。" >
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<RadioButton x:Name="radioBig" IsChecked="True" Content="大きくなれ" Margin="10,5"/>
<RadioButton x:Name="radioSmall" Content="小さくなれ" Margin="10,5" />
<CheckBox x:Name="stopAnimationCheckBox" Content="アニメーション停止"
HorizontalAlignment="Center" Margin="10,5" />
</StackPanel>
</Border>
<Button Name="button1" Grid.Column="0" Grid.Row="1"
Width="90" Height="20" Content="おっきくなるよ"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Window>
"""
class ExWindow(Object):
def __init__(self):
win = XamlReader.Parse(xaml_str)
self.Root = win
self.setAttr("button1",win)
self.setAttr("stopAnimationCheckBox",win)
self.setAttr("radioBig",win)
self.setAttr("radioSmall",win)
self.radioBig.Checked += self.radioBig_Checked
self.radioSmall.Checked += self.radioSmall_Checked
self.EXPAND = ( 50.0, ) # タップル 変更不可能
#// アニメーションを行うコントロールをNameScopeに登録
#NameScope.SetNameScope(win, NameScope())
#win.RegisterName(self.button1.Name, self.button1)
#すでに、登録されているから...
self.CreateExpandAnimation()
def setAttr(self, name, xaml):
x = xaml.FindName(name)
if x != None : setattr(self, name, x)
else: print "NameError: '%s' object has no Name '%s'" %(type(xaml),name)
def CreateExpandAnimation(this):
#// ボタンの幅を拡大するアニメーション
#// Byは動的に値を設定するためClickイベントで設定
this._expandWidthAnimation = DoubleAnimation()
this._expandWidthAnimation.Duration = Duration(TimeSpan.FromMilliseconds(500))
#// ボタンの高さを拡大するアニメーション
#// Byは動的に値を設定するためClickイベントで設定
this._expandHeightAnimation = DoubleAnimation()
this._expandHeightAnimation.Duration = Duration(TimeSpan.FromMilliseconds(500))
#// ストーリーボードを作成
this._expandStoryboard = Storyboard()
this._expandStoryboard.Children.Add(this._expandWidthAnimation)
Storyboard.SetTargetName(this._expandWidthAnimation, this.button1.Name)
Storyboard.SetTargetProperty(
this._expandWidthAnimation,
PropertyPath(Button.WidthProperty))
this._expandStoryboard.Children.Add(this._expandHeightAnimation)
Storyboard.SetTargetName(this._expandHeightAnimation, this.button1.Name)
Storyboard.SetTargetProperty(
this._expandHeightAnimation,
PropertyPath(Button.HeightProperty))
#// イベントハンドラ追加
this.button1.Click += this.button1_Click
def button1_Click(self, sender, e):
if (self.stopAnimationCheckBox.IsChecked == False):
if (self.radioBig.IsChecked == True): by = self.EXPAND[0]
else:
if ( self.button1.Height <= self.EXPAND[0] ) : by = 0.0
else: by = -self.EXPAND[0]
if by != 0.0 :
self._expandWidthAnimation.By = by
self._expandHeightAnimation.By = by
self._expandStoryboard.Begin(self.Root)
def radioBig_Checked(self, sender, e):
self.button1.Content = "大きくなるよ"
def radioSmall_Checked(self, sender, e):
self.button1.Content = "小さくなるよ"
if __name__ == "__main__":
win = ExWindow()
Application().Run(win.Root)
![]() | IronPythonの世界 (Windows Script Programming) |
荒井 省三 | |
ソフトバンク クリエイティブ |
![]() | エキスパートPythonプログラミング |
Tarek Ziade | |
アスキー・メディアワークス |
![]() | Pythonスタートブック |
辻 真吾 | |
技術評論社 |