myBlog

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

IronPythonでColorComboBoxをつくりました

2011-07-01 23:21:17 | IronPython

IronPythonで色を選択するComboBoxをつくりました.。

XAML と IronPythonを1つのファイルに、まとめています。

Colorcombobox

#
# ColorComboBox.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, CornerRadius, Thickness
from System.Windows.Controls import (StackPanel, Border, Orientation,
                    ComboBoxItem, ComboBoxItem, TextBlock )
from System.Windows.Media import Color, Colors, SolidColorBrush, Brushes

xaml_str="""
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Color ComboBox" Height="62" Width="220" >
  <ComboBox Name="comboBox1" Height ="25" MaxDropDownHeight="338"/>
</Window>
"""

class ExWindow(Object):
    def __init__(self):
        win = XamlReader.Parse(xaml_str)
        self.Root = win
        self.comboBox1 = win.FindName("comboBox1")
        win.Loaded += self.window1_Loaded

    def window1_Loaded(self, sender, e):
        self.SetupComboBox()
        if (self.comboBox1.Items.Count > 0):
           self.comboBox1.SelectedIndex = 0

    def SetupComboBox(self):
        item = ComboBoxItem()

        infs=[]
        for name in dir(Colors):
            c = getattr(Colors, name)
            if isinstance(c, Color):
                infs.append([name, SolidColorBrush(c)])

        for name, col in infs:
            brush = col
            item = ComboBoxItem()

            panel = StackPanel()
            panel.Orientation = Orientation.Horizontal

            border = Border()
            border.Background = brush
            border.CornerRadius = CornerRadius(3)
            border.Width = 30
            border.Height = 12
            border.BorderBrush = Brushes.Black
            border.BorderThickness = Thickness(1)

            block = TextBlock()
            block.Text = name
            block.Width = self.comboBox1.Width - border.Width - 20
            block.Margin = Thickness(10, 0, 0, 0)

            panel.Children.Add(border)
            panel.Children.Add(block)

            item.Content = panel
            self.comboBox1.Items.Add(item)

if __name__ == "__main__":
    win = ExWindow()
    Application().Run(win.Root)

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

最新の画像もっと見る

コメントを投稿