WPF使用IronPython庫的簡單Demo
WPF使用IronPython庫的簡單Demo
一、IronPython和C#互動
IronPython是一個.NET平臺上的Python實現,包括了完整的編譯器、執行引擎與執行時支援,能夠與.NET已有的庫無縫整合到一起。
IronPython已經很好的整合到了.NET framework中,所以Ironpython和C#的互動也就變得很簡單了。下面就通過一些簡單的例子來看看IronPython和C#之間的互動。
二、PTVS(Python tools for Visual Studio)
是一個免費開源的VisualStudio的外掛,支援 VisualStudio 2010/2012/2013,安裝好這個外掛之後,我們就可以直接通過VS進行IronPython的開發了。可以使用其提供的IronPython WPF模板直接生成WPF的IronPython專案,這個示例在
三、WPF with IronPython
1、使用VS2019建立WPF專案
引用IronPython相關庫,IronPython庫可以直接在GitHub上下載
2、編輯Xaml檔案
<Window x:Class="ScriptEditor.MainWindow" 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:ScriptEditor" mc:Ignorable="d" Title="Calculator" Height="450" Width="300" ResizeMode="CanMinimize"> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock Name="InputTb" Grid.Row="0" Grid.ColumnSpan="4" VerticalAlignment="Center" FontSize="16"/> <TextBlock Name="ResultTb" Grid.Row="1" Grid.ColumnSpan="3" VerticalAlignment="Center" FontSize="16"/> <Button Content="1" Grid.Row="2" Grid.Column="0" Click="Input_Button_Click"/> <Button Content="2" Grid.Row="2" Grid.Column="1" Click="Input_Button_Click"/> <Button Content="3" Grid.Row="2" Grid.Column="2" Click="Input_Button_Click"/> <Button Content="4" Grid.Row="3" Grid.Column="0" Click="Input_Button_Click"/> <Button Content="5" Grid.Row="3" Grid.Column="1" Click="Input_Button_Click"/> <Button Content="6" Grid.Row="3" Grid.Column="2" Click="Input_Button_Click"/> <Button Content="7" Grid.Row="4" Grid.Column="0" Click="Input_Button_Click"/> <Button Content="8" Grid.Row="4" Grid.Column="1" Click="Input_Button_Click"/> <Button Content="9" Grid.Row="4" Grid.Column="2" Click="Input_Button_Click"/> <Button Content="0" Grid.Row="5" Grid.Column="0" Click="Input_Button_Click"/> <Button Content="+" Grid.Row="2" Grid.Column="3" Click="Input_Button_Click"/> <Button Content="-" Grid.Row="3" Grid.Column="3" Click="Input_Button_Click"/> <Button Content="*" Grid.Row="4" Grid.Column="3" Click="Input_Button_Click"/> <Button Content="/" Grid.Row="5" Grid.Column="3" Click="Input_Button_Click"/> <Button Content="." Grid.Row="5" Grid.Column="1" Click="Input_Button_Click"/> <Button Content="C" Grid.Row="5" Grid.Column="2" Click="Clear_Button_Click"/> <Button Content="=" Grid.Row="1" Grid.Column="3" Click="Calc_Button_Click"/> </Grid> </Window>
3、編輯Xmal後臺檔案
(在這個demo裡沒有使用MVVM模式),所以直接在後臺寫邏輯
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ScriptEditor
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
ScriptRuntime pyRunTime;
dynamic obj;
public MainWindow()
{
InitializeComponent();
}
private void Input_Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
this.InputTb.Text += button.Content;
}
private void Clear_Button_Click(object sender, RoutedEventArgs e)
{
this.InputTb.Text = "";
this.ResultTb.Text = "";
}
private void Calc_Button_Click(object sender, RoutedEventArgs e)
{
try
{
if (obj==null)
{
//建立Python執行時
pyRunTime = Python.CreateRuntime();
//建立動態物件
obj = pyRunTime.UseFile(@"Script/Calculation.py");
}
//呼叫Python函式方法
this.ResultTb.Text = obj.Calc_Button_Click(this.InputTb.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
4、編輯Python指令碼
因為Python擁有許多強大的庫,以及它的靈活的特點,在做一些小型的專案開發可以利用他的輕便性,對於一些大型專案需要也可以使用它進行靈活的配置
def Calc_Button_Click(expression):
try:
result = eval(expression)
return str(result)
except Exception, e:
tracelog = traceback.format_exc()
pass
5、執行結果
以上就是一個非常簡單的例子了,因為使用了動態Dynamic物件,實際上的效能(響應速度偏慢)不好,但如果只是需要用到它做一個擴充套件功能,還是很方便的
IronPython還有許多其他方面的問題,比如目前不支援Python3.X,當Python內Import了一些其他的庫的時候會報找不到模組的錯誤,這個解決辦法在:https://www.cnblogs.com/monkeyfx/p/6522000.html 中有詳細的解決方案,各位可以參考