日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python wpf教程_WPF使用IronPython库的简单Demo

發布時間:2023/12/19 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python wpf教程_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項目,這個示例在 https://blog.csdn.net/sinat_27382047/article/details/79938003 里有詳細的介紹,但是由于我使用這個項目模板做WPF開發時,總是存在一些這樣那樣的問題,并且該模板對于應用比較復雜的WPF項目來說不是那么的友好,并且不太好支持MVVM模式,所以我采用的是直接在傳統的WPF項目中使用IronPython庫實現調用Python腳本的方法,下面的例子2,是使用的一個Demo。

三、WPF with IronPython

1、使用VS2019創建WPF項目

引用IronPython相關庫,IronPython庫可以直接在GitHub上下載

2、編輯Xaml文件

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">

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

{

///

/// MainWindow.xaml 的交互邏輯

///

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 中有詳細的解決方案,各位可以參考

總結

以上是生活随笔為你收集整理的python wpf教程_WPF使用IronPython库的简单Demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。