myBlog

IronPython, Silverlight, WPF, XAML, HTML5, ...

IronPythonで、Gestalt の Sample を動かす! 《 WPF/IronPython 》

2011-07-11 18:33:05 | Animation
WPF/IronPythonで、Gestalt の マッシュルームのサンプルを実行する。

久しぶりに、ネットからGestaltのSampleを動かそうとしたが、起動できなかった。
そこで、IronPythonで、動かすことにした。無事、完成しました!

Gestalt ⇒ samples ⇒ Getting Started Pack ⇒ view 5
http://www.visitmix.com/labs/gestalt/samples/

blinking_mushroo XAML ⇒ mushroom.xaml ⇒ マッシュルーム
http://softgarden.lovepop.jp/myBlog/xaml/mushroom.xaml

Gestaltsample_2

#
# GestaltSample.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application
from System.Windows.Controls import Canvas, Label, Button, TextBox

def LoadXamlNet(strUrl):
    import System
    request = System.Net.WebRequest.Create(strUrl)
    #request.Credentials = System.Net.CredentialCache.DefaultCredentials
    response = request.GetResponse()
    dataStream = response.GetResponseStream()
    try:
        element = XamlReader.Load(dataStream)
    finally:
        dataStream.Close()
        response.Close()
    return element

class ExWindow(Window):
    def __init__(self):
        self.Title="GestaltSample.py"
        self.Width = 235
        self.Height= 360
        self.Content = canvas = Canvas()
        self.say_hello_lbl = Label(Content="Say hello to: " ,FontSize = 16 )
        self.say_hello_txt = TextBox(Width=90.0,FontSize = 16)
        self.say_hello_btn = Button(Content="Go",FontSize = 16, Width = 30)
        canvas.AddChild(self.say_hello_lbl)
        canvas.AddChild(self.say_hello_txt)
        canvas.AddChild(self.say_hello_btn)
        #bm = me.blinking_mushroom
        #bm.left_eye_blink.Begin()
        #bm.right_eye_blink.Begin()
        self.blinking_mushroom = bm = LoadXamlNet("http://softgarden.lovepop.jp/myBlog/xaml/mushroom.xaml")
        canvas.AddChild(bm)
        bm.FindName("left_eye_blink").Begin()
        bm.FindName("right_eye_blink").Begin()
        self.show_bubble = bm.FindName("show_bubble")
        self.float_bubble = bm.FindName("float_bubble")
        self.bubble_text = bm.FindName("bubble_text")
        self.say_hello_btn.Click += self.OnClick
        self.show_bubble.Completed += self.Completed

        self.SetPosition(self.say_hello_lbl, 5, 8) 
        self.SetPosition(self.say_hello_txt, 95, 10) 
        self.SetPosition(self.say_hello_btn, 190, 9)
        self.SetPosition(self.blinking_mushroom, 10, 35) 

    def SetPosition(self, obj, x, y):
        try:
            Canvas.SetTop(obj, y)
        except: pass
        try:
            Canvas.SetLeft(obj, x)
        except: pass
    def OnClick(self, s, e):
        self.bubble_text.Text = "Hello, " + self.say_hello_txt.Text + "!"
        self.show_bubble.Begin()
    def Completed(self, s, e):
        self.float_bubble.Begin()

if __name__ == "__main__":
    win = ExWindow()
    Application().Run(win)
IronPythonの世界 (Windows Script Programming)
荒井 省三
ソフトバンク クリエイティブ
エキスパートPythonプログラミング
Tarek Ziade
アスキー・メディアワークス
Pythonスタートブック
辻 真吾
技術評論社

最新の画像もっと見る

コメントを投稿