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

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#一样.

cs 复制代码
using System;
using SC = System.Console;

是不是一模一样;

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

python 复制代码
class MainWindow(Window):
 
  def __init__(self):
    Window.__init__(self)
 
    #  XAML
    xaml = """
    <Page
      xmlns="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 Button1
    self.Button1 = LogicalTreeHelper.FindLogicalNode(page, "Button1")
    self.Button1.Click += self.Button1_Click
 
    # connect Button2
    self.Button2 = LogicalTreeHelper.FindLogicalNode(page, "Button2")
    self.Button2.Click += self.Button2_Click
 
    # 
    self.Title = "Python WPF App with XAML!"
    self.Width = 350
    self.Height = 300
    self.Content = page
 
  def 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.STA
  t.Start()
  t.Join()
 
if __name__ == "__main__":
  main()

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

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

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

相关推荐
Freak嵌入式6 分钟前
小作坊 GitHub 协作闭环:fork-sync-dev-pr-merge 实战指南
python·github·远程工作·代码规范·micropython·协作
跟着珅聪学java8 分钟前
在 Java 中处理 JSON 去除空 children数组,可以使用 Jackson 库。这里有几种实现方式
开发语言·windows·python
Chase_______21 分钟前
【Python基础 | 第5章】面向对象与异常处理:一文搞懂类、对象、封装、继承、多态
开发语言·python
YanDDDeat21 分钟前
【大模型微调】基于 Llama3-8B 的 LoRA 微调专有领域QA 问答对生成模型
python·语言模型·llama
小李云雾23 分钟前
Python Web 路由详解:核心知识点全覆盖
开发语言·前端·python·路由
翻斗包菜32 分钟前
零基础入门 Flask 框架
后端·python·flask
平安的平安32 分钟前
Python 实现 AI 图像生成:调用 Stable Diffusion API 完整教程
人工智能·python·stable diffusion
_下雨天.34 分钟前
Flask 框架
后端·python·flask
卤炖阑尾炎35 分钟前
Flask 框架实战全解:从入门到精通
后端·python·flask
tang7778936 分钟前
小红书平台用什么代理IP?数据采集IP封禁解决方法
数据库·爬虫·python·网络协议·ip