python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML

devtools/2025/2/23 23:54:46/

pythonnet 是pythonhe.net通用的神器不多介绍了.  

这次这基本上跟python没有关系了.

和winform一样先导包

python">import clr
clr.AddReference("PresentationFramework.Classic, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
clr.AddReference("PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")from System.Windows import Application
from System.Windows import Window
from System.Windows import MessageBox
from System.Windows import LogicalTreeHelper
from System.Windows.Markup import XamlReader
from System.Threading import Thread
from System.Threading import ApartmentState
from System.Threading import ThreadStart
from System import *

哈哈哈 有没有感觉和 C#一样.

using System;
using SC = System.Console;

是不是一模一样;

接下来:  写个类.  当然你可以用C#  VB.net单独做 成DLL也没关系. 重要的是先把婚结了 孩子生出来.  不对 先把东西生产出来.

python">class MainWindow(Window):def __init__(self):Window.__init__(self)#  XAMLxaml = """<Pagexmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Grid>  <Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Button Name="Button1" Grid.Row="0">Button 1</Button><Button Name="Button2" Grid.Row="1">Button 2</Button></Grid></Page>"""page = XamlReader.Parse(xaml)# connect Button1self.Button1 = LogicalTreeHelper.FindLogicalNode(page, "Button1")self.Button1.Click += self.Button1_Click# connect Button2self.Button2 = LogicalTreeHelper.FindLogicalNode(page, "Button2")self.Button2.Click += self.Button2_Click# self.Title = "Python WPF App with XAML!"self.Width = 350self.Height = 300self.Content = pagedef Button1_Click(self, sender, e):MessageBox.Show(self.Button1.Content + " is clicked!")def Button2_Click(self, sender, e):MessageBox.Show(self.Button2.Content + " is clicked!")

python">def STAMain():app = Application()app.Run(MainWindow())def main():t = Thread(ThreadStart(STAMain))t.ApartmentState = ApartmentState.STAt.Start()t.Join()if __name__ == "__main__":main()

界面一定要线程里面; 要不然python解释器会卡死.

当然 你也可以这么干;用C#界面类库打包写成DLL在用python调用. python只管传参数.

对于性能要求不高的场合就很合适.


http://www.ppmy.cn/devtools/161247.html

相关文章

我的电脑是 3070ti 能用那个级别的deepseek

你的电脑配备了 NVIDIA 3070 Ti,这是一款性能较强的消费级显卡,拥有 8GB 显存。虽然它的性能无法与专业级 GPU(如 A100 或 H100)相比,但仍然可以用于运行和微调一些中小规模的深度学习模型,包括 DeepSeek 的相关任务。 以下是基于你的硬件配置,可以运行的 DeepSeek 模型…

本地化部署 DeepSeek:从零到一的完整指南

本地化部署 DeepSeek&#xff1a;从零到一的完整指南 个人主页&#xff1a;顾漂亮 文章专栏&#xff1a;AI学习 目录 引言什么是 DeepSeek&#xff1f;为什么选择本地化部署&#xff1f;DeepSeek 本地化部署的前期准备 硬件需求软件需求环境配置 DeepSeek 本地化部署步骤 步骤…

Vue框架中Ajax请求的实现方式:使用axios库或fetch API

在Vue框架中&#xff0c;Ajax请求是前后端交互的重要手段&#xff0c;用于异步获取数据。Vue本身不包括Ajax功能&#xff0c;但可以通过引入外部库来实现。常用的有 axios库和原生的 fetch API。下面将详细介绍这两种实现方式&#xff0c;以及它们的使用示例。 使用Axios库 A…

算法日记24:leetcode198打家劫舍(DFS->记忆化搜索->倒序动态规划->循序动态规划)

一、递归写法&#xff08;dfs深搜&#xff09; 1.1&#xff09;思路讲解 递归思想&#xff1a; dfs(x)表示从第x家店开始的最大劫掠值。对每一家店铺&#xff0c;有两个选择&#xff1a; 不劫掠 当前店铺&#xff0c;即跳到下家 dfs(x1)。劫掠 当前店铺&#xff0c;且跳过下家…

DeepSeek掘金——SpringBoot 调用 DeepSeek API 快速实现应用开发

Spring Boot 实现 DeepSeek API 调用 1. 项目依赖 在 pom.xml 中添加以下依赖: <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency>&l…

Windows 系统下,使用 PyTorch 的 DataLoader 时,如果 num_workers 参数设置为大于 0 的值,报错

在 Windows 系统下&#xff0c;使用 PyTorch 的 DataLoader 时&#xff0c;如果 num_workers 参数设置为大于 0 的值&#xff0c;可能会遇到以下错误&#xff1a; RuntimeError: An attempt has been made to start a new process before thecurrent process has finished its…

7.日常英语笔记

sweep the floor扫地 mop the floor 拖地 wipe the table 擦桌子 dust the furniture 掸灰 make the bed 铺床 polish the furniture 把家具擦亮 fold the clothes 叠衣服 iron the clothes 熨衣服 wash the dishes 洗碗 dry the dishes 擦干餐具 empty the trash 倒垃圾 tidy …

提升信息检索准确性和效率的搜索技巧

一、基础技巧 精准关键词 避免长句子&#xff0c;提取核心关键词&#xff08;如用“光合作用 步骤”代替“请告诉我光合作用的具体过程”&#xff09;。 同义词替换&#xff1a;尝试不同表达&#xff08;如“AI 发展史” vs “人工智能 历史”&#xff09;。 排除干扰词 使用…