Please help with WPF.
I try to learn more about wpf and xaml for UI in my scripts
I found really great examples on GitHub from @c.poupin . Thanks for that! so much
I tried to make a simple form with dropdown box where you can select a needed type.
and when you change selected item from list - image as well should changed for current selected item
some code below:
import clr
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\DLLs')
try:
clr.AddReference("IronPython.Wpf")
clr.AddReference('System.Core')
clr.AddReference('System.Xml')
clr.AddReference('PresentationCore')
clr.AddReference('PresentationFramework')
except IOError:
raise
from System.IO import StringReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application
from System import Uri
from System.Windows.Media.Imaging import BitmapImage
try:
import wpf
except ImportError:
raise
class DropdownInput(Window):
LAYOUT = """
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Height="500"
Width="320"
ResizeMode="NoResize"
Title="A"
WindowStartupLocation="CenterScreen"
Topmost="True"
SizeToContent="Width">
<Grid Margin="10,0,10,10">
<Label x:Name="selection_label" Content="Select Item" HorizontalAlignment="Left" Height="30"
VerticalAlignment="Top"/>
<ComboBox x:Name="combo_data" HorizontalAlignment="Left" Margin="0,30,0,0"
VerticalAlignment="Top" Width="300"/>
<Button x:Name="button_select" Content="Select" HorizontalAlignment="Left" Height="26"
Margin="0,63,0,0" VerticalAlignment="Bottom" Width="300"/>
<Image x:Name="img" HorizontalAlignment="Center" Height="316" Margin="10,70,10,10" VerticalAlignment="Top" Width="260" Source="" Stretch="Fill" IsEnabled="True"/>
</Grid>
</Window>
"""
def __init__(self, title, options, description=None, uris="C://Railing/Source/2.png"):
self.selected = None
self.ui = wpf.LoadComponent(self, StringReader(DropdownInput.LAYOUT))
self.ui.Title = title
self.ui.selection_label.Content = description
self.ui.button_select.Click += self.select_click
self.ui.combo_data.Items.Clear()
self.ui.combo_data.ItemsSource = options
self.ui.combo_data.SelectedItem = options[-1]
self.ui.img.Source = BitmapImage(Uri(uris))
def select_click(self, sender, e):
self.selected = self.ui.combo_data.SelectedItem
self.DialogResult = True
self.Close()
run = True
keys = ["A", "B", "C"]
values = [1, 2, 3]
description = "smth"
uri1 = ["C://Railing/Source/1.png", "C://Railing/Source/2.png", "C://Railing/Source/3.png"]
if values:
if len(values) != len(keys):
raise Exception('Length of Values and Keys must match')
dropdown_options = {k:u for k, v, u in zip(keys, values, uri1)}
else:
dropdown_options = {k:k for k in keys}
if run:
form = DropdownInput('InputForm', dropdown_options.keys(), description=description, uris=dropdown_options.uri1())
form.ShowDialog()
if form.selected is not None:
OUT = dropdown_options[form.selected]
else:
OUT = 'Set RUN to True'