2020년 12월 5일 토요일

[IronPython] WPF Button Control

WPF Button Control

import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore");
clr.AddReference('System.Data')
clr.AddReference('System.Windows.Forms')

import System
from System import *
from System.Data import *
from System.Type import GetType
from System.Threading.Tasks import Task
from System.Windows import *
from System.Windows.Controls import *
from System.Windows.Controls.Primitives import *
from System.Windows.Media import *
from System.Windows.Media.Imaging import *
from System.Windows.Media.Effects import *
from System.Windows.Shapes import Rectangle
from System.Windows.Data import Binding
from System.Windows.Ink import Stroke
from System.Windows.Forms import ( FolderBrowserDialog, DialogResult )
from Microsoft.Win32 import ( OpenFileDialog, SaveFileDialog )

import ctrl

class WpfButton(ctrl.WpfCtrl):
    def __init__(self,text=None,handler=None,image=None,size=None,vert=True,**kwargs):
        self.ctrl = Button()
        self.init(**kwargs)
        stack = StackPanel()
        if vert: stack.Orientation = Orientation.Vertical
        else: stack.Orientation = Orientation.Horizontal
        self.ctrl.Content = stack
        if image:
            img = Image()
            img.Source = BitmapImage(System.Uri(image,System.UriKind.Relative))
            img.VerticalAlignment = VerticalAlignment.Center
            img.Stretch = Stretch.Fill #Stretch.None
            if size: img.Width = size; img.Height = size
            stack.Children.Add(img)
        if text: 
            txt = TextBlock()
            txt.Text = text
            txt.TextAlignment = TextAlignment.Center
            stack.Children.Add(txt)         
        if handler: self.ctrl.Click += handler

if __name__ == "__main__":
    import win
    app = win.Win("Button Demo",320,240)
    b = WpfButton("Exit", handler=lambda s,e: app.Close())
    app.set_content(b.ctrl)
    app.run()

댓글 없음:

댓글 쓰기