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

news/2025/2/24 3:03:44/

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/news/1574529.html

相关文章

ubuntu部署小笔记-采坑

ubuntu部署小笔记 搭建前端控制端后端前端nginx反向代理使用ubuntu部署nextjs项目问题一 如何访问端口号配置后台运行该进程pm2 问题二 包体过大生产环境下所需文件 问题三 部署在vercel时出现的问题需要魔法访问后端api时&#xff0c;必须使用https协议电脑端访问正常&#xf…

20250220-代码笔记01-class CVRPEnv

文章目录 前言一、def __init__(self, **env_params)&#xff1a;函数功能函数代码 二、use_saved_problems(self, filename, device)函数功能函数代码 三、load_problems(self, batch_size, aug_factor1)函数功能函数代码use_saved_problems 与 load_problems 之间的关系 四、…

【论文笔记】Mamba: Linear-time sequence modeling with selective state spaces

【引用格式】&#xff1a;Gu A, Dao T. Mamba: Linear-time sequence modeling with selective state spaces[J]. arXiv preprint arXiv:2312.00752, 2023. 【网址】&#xff1a;https://arxiv.org/pdf/2312.00752 【开源代码】&#xff1a;https://github.com/state-spaces/…

探索火山引擎 DeepSeek-R1 满血版:流畅、高效的 AI 开发体验

方舟大模型体验中心全新上线&#xff0c;免登录体验满血联网版Deep Seek R1 模型及豆包最新版模型》 https://www.volcengine.com/experience/ark?utm_term202502dsinvite&acDSASUQY5&rcA4K514ZC 大家好&#xff01;作为一名开发者&#xff0c;我一直在寻找能够提升…

Spring Boot 中多线程工具类的配置与使用:基于 YAML 配置文件

文章目录 Spring Boot 中多线程工具类的配置与使用&#xff1a;基于 YAML 配置文件1. 为什么需要多线程工具类&#xff1f;2. 实现步骤2.1 添加依赖2.2 配置线程池参数2.3 创建配置类2.4 创建线程池工具类2.5 使用线程池工具类2.6 测试线程池工具类 3. 配置文件的灵活性4. 总结…

低空经济应用场景细分赛道探索,无人机开源飞控二次开发详解

低空经济应用场景细分赛道探索 低空经济作为一个新兴的经济形态&#xff0c;随着直升机、eVTOL&#xff08;电动垂直起降飞行器&#xff09;、无人机等新技术新产品的快速发展&#xff0c;其应用范围正向第一、第二、第三产业中的多个领域迅速拓展。以下是对低空经济应用场景细…

蓝桥杯 Java B 组之岛屿数量、二叉树路径和(区分DFS与回溯)

Day 3&#xff1a;岛屿数量、二叉树路径和&#xff08;区分DFS与回溯&#xff09; &#x1f4d6; 一、深度优先搜索&#xff08;DFS&#xff09;简介 深度优先搜索&#xff08;Depth-First Search&#xff0c;DFS&#xff09;是一种用于遍历或搜索树或图的算法。它会沿着树的分…

【洛谷排序算法】P1012拼数-详细讲解

洛谷 P1012 拼数这道题本身并非单纯考察某种经典排序算法&#xff08;如冒泡排序、选择排序、插入排序、快速排序、归并排序等&#xff09;的实现&#xff0c;而是在排序的基础上&#xff0c;自定义了排序的比较规则&#xff0c;属于自定义排序类型的题目。不过它借助了标准库中…