IronPython WPF サンプルの calc を myBlog用に変換しました。
下記のBlogを参考にして、Drag可能な、電卓(calc)にしました。
Blog: jimmy.thinking ⇒ Dragging elements in Silverlight with DLRConsole
http://blog.jimmy.schementi.com/2008/08/dragging-elements-in-silverlight-with.html
MSDN Blogs ⇒ 荒井省三のBlog ⇒ DLR Console を使って時計をドラッグするサンプル
http://blogs.msdn.com/b/shozoa/archive/2008/09/03/dragging-clock-on-dlr-console.aspx
下記のBlogを参考にして、Drag可能な、電卓(calc)にしました。
Blog: jimmy.thinking ⇒ Dragging elements in Silverlight with DLRConsole
http://blog.jimmy.schementi.com/2008/08/dragging-elements-in-silverlight-with.html
MSDN Blogs ⇒ 荒井省三のBlog ⇒ DLR Console を使って時計をドラッグするサンプル
http://blogs.msdn.com/b/shozoa/archive/2008/09/03/dragging-clock-on-dlr-console.aspx
Calc XAML⇒calc.xaml
⇒ 電卓(calc)
http://softgarden.lovepop.jp/myBlog/xaml/calc.xaml
#####################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# This source code is subject to terms and conditions of the Microsoft Public License. A
# copy of the license can be found in the License.html file at the root of this distribution. If
# you cannot locate the Microsoft Public License, please send an email to
# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
# by the terms of the Microsoft Public License.
#
# You must not remove this notice, or any other, from this software.
#
#
#####################################################################################
#
# calculator.py
#
from System.Windows.Controls import *
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
def enliven(w):
try:
controls = [ n for n in Walk(w) if isinstance(n, Button) or isinstance(n, TextBox) ]
Calculator(controls)
except:
print "Function failed. Did you pass in the Calculator window?"
class Calculator:
def __init__(self, controls):
self.expression = ""
for i in controls:
if isinstance(i, Button):
if hasattr(self, "on_" + i.Name):
print "Registering self.on_" + i.Name + " to handle " + i.Name + ".Click"
i.Click += getattr(self, "on_" + i.Name)
elif isinstance(i, TextBox):
if i.Name == "Result":
self.result = i
self.result.Text = self.expression
def on_Button(self, c):
self.expression += c
self.result.Text = self.expression
def on_Clear(self, b, e):
self.expression = ""
self.result.Text = self.expression
def on_Equals(self, b, e):
try:
result = str(eval(self.expression))
self.result.Text = result
self.expression = result
except:
self.result.Text = "<<ERROR>>"
self.expression = ""
def on_One(self, b, e):
self.on_Button('1')
def on_Nine(self, b, e):
self.on_Button('9')
def on_Eight(self, b, e):
self.on_Button('8')
def on_Five(self, b, e):
self.on_Button('5')
def on_Four(self, b, e):
self.on_Button('4')
def on_Two(self, b, e):
self.on_Button('2')
def on_Three(self, b, e):
self.on_Button('3')
def on_Six(self, b, e):
self.on_Button('6')
def on_Multiply(self, b, e):
self.on_Button('*')
def on_Seven(self, b, e):
self.on_Button('7')
def on_Subtract(self, b, e):
self.on_Button('-')
def on_Zero(self, b, e):
self.on_Button('0')
def on_DecimalPoint(self, b, e):
self.on_Button('.')
def on_Plus(self, b, e):
self.on_Button('+')
def on_Divide(self, b, e):
self.on_Button('/')
#
# calc.py
# ipy.exe calc.py
# ipyw.exe calc.py
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase')
from System import Object
from System.Windows.Markup import XamlReader
from System.Windows import Window, Application
from System.Windows.Controls import Canvas, Button, TextBox
from System.Windows.Media import Brushes
import calculator
def LoadXamlNet(strUrl):
import System
request = System.Net.WebRequest.Create(strUrl)
response = request.GetResponse()
dataStream = response.GetResponseStream()
try:
element = XamlReader.Load(dataStream)
finally:
dataStream.Close()
response.Close()
return element
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
class Calc(Object):
def __init__(self):
self.scene = calc = LoadXamlNet("http://softgarden.lovepop.jp/myBlog/xaml/calc.xaml")
controls = [ n for n in Walk(calc) if isinstance(n, Button) or isinstance(n, TextBox) ]
for c in controls:
c.FontSize *=2
calculator.enliven(calc)
class Drag(object):
def __init__(self, root, obj):
self.click = None
self.obj = obj
self.root = root
def OnMouseLeftButtonDown(self, sender, e):
self.click = e.GetPosition(self.root)
self.sx = Canvas.GetLeft(self.obj)
self.sy = Canvas.GetTop(self.obj)
if (self.sx.IsNaN(self.sx)): self.sx = 0.0
if (self.sy.IsNaN(self.sy)): self.sy = 0.0
self.obj.CaptureMouse()
def OnMouseLeftButtonUp(self, sender, e):
if(self.click != None):
self.obj.ReleaseMouseCapture()
self.click = None
def OnMouseMove(self, sender, e):
if(self.click != None):
mouse_pos = e.GetPosition(self.root)
Canvas.SetLeft(self.obj, (self.sx + mouse_pos.X - self.click.X))
Canvas.SetTop(self.obj, (self.sy + mouse_pos.Y - self.click.Y))
def enable(self):
self.obj.MouseLeftButtonDown += self.OnMouseLeftButtonDown
self.obj.MouseLeftButtonUp += self.OnMouseLeftButtonUp
self.obj.MouseMove += self.OnMouseMove
def disable(self):
self.obj.MouseLeftButtonDown -= self.OnMouseLeftButtonDown
self.obj.MouseLeftButtonUp -= self.OnMouseLeftButtonUp
self.obj.MouseMove -= self.OnMouseMove
class ExWindow(Window):
def __init__(self):
self.Title = "Dynamic Languages Rock!"
self.Width = 600
self.Height = 480
self.Content = canvas = Canvas(Background = Brushes.LightGray)
self.calc = Calc().scene
canvas.Children.Add (self.calc)
Canvas.SetTop(self.calc, 60)
Canvas.SetLeft(self.calc, 180)
self.drag = Drag(root=canvas, obj=self.calc)
self.drag.enable()
if __name__ == "__main__":
win = ExWindow()
Application().Run(win)
![]() | IronPythonの世界 (Windows Script Programming) |
荒井 省三 | |
ソフトバンク クリエイティブ |
![]() | エキスパートPythonプログラミング |
Tarek Ziade | |
アスキー・メディアワークス |
![]() | Pythonスタートブック |
辻 真吾 | |
技術評論社 |
※コメント投稿者のブログIDはブログ作成者のみに通知されます