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

打印 上一主题 下一主题

主题 906|帖子 906|积分 2718

pythonnet 是pythonhe.net通用的神器不多先容了.  
这次这基本上跟python没有关系了.
和winform一样先导包
  1. import clr
  2. clr.AddReference("PresentationFramework.Classic, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
  3. clr.AddReference("PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
  4. from System.Windows import Application
  5. from System.Windows import Window
  6. from System.Windows import MessageBox
  7. from System.Windows import LogicalTreeHelper
  8. from System.Windows.Markup import XamlReader
  9. from System.Threading import Thread
  10. from System.Threading import ApartmentState
  11. from System.Threading import ThreadStart
  12. from System import *
复制代码
哈哈哈 有没有感觉和 C#一样.
  1. using System;
  2. using SC = System.Console;
复制代码
是不是千篇一律;
接下来:  写个类.  固然你可以用C#  VB.net单独做 成DLL也没关系. 重要的是先把婚结了 孩子生出来.  不对 先把东西生产出来.
  1. class MainWindow(Window):
  2.   def __init__(self):
  3.     Window.__init__(self)
  4.     #  XAML
  5.     xaml = """
  6.     <Page
  7.       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  8.       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  9.       <Grid>  
  10.       <Grid.RowDefinitions>
  11.         <RowDefinition/>
  12.         <RowDefinition/>
  13.       </Grid.RowDefinitions>
  14.       <Button Name="Button1" Grid.Row="0">Button 1</Button>
  15.       <Button Name="Button2" Grid.Row="1">Button 2</Button>
  16.       </Grid>
  17.     </Page>
  18.     """
  19.     page = XamlReader.Parse(xaml)
  20.     # connect Button1
  21.     self.Button1 = LogicalTreeHelper.FindLogicalNode(page, "Button1")
  22.     self.Button1.Click += self.Button1_Click
  23.     # connect Button2
  24.     self.Button2 = LogicalTreeHelper.FindLogicalNode(page, "Button2")
  25.     self.Button2.Click += self.Button2_Click
  26.     #
  27.     self.Title = "Python WPF App with XAML!"
  28.     self.Width = 350
  29.     self.Height = 300
  30.     self.Content = page
  31.   def Button1_Click(self, sender, e):
  32.     MessageBox.Show(self.Button1.Content + " is clicked!")
  33.   def Button2_Click(self, sender, e):
  34.     MessageBox.Show(self.Button2.Content + " is clicked!")
复制代码

  1. def STAMain():
  2.   app = Application()
  3.   app.Run(MainWindow())
  4. def main():
  5.   t = Thread(ThreadStart(STAMain))
  6.   t.ApartmentState = ApartmentState.STA
  7.   t.Start()
  8.   t.Join()
  9. if __name__ == "__main__":
  10.   main()
复制代码
界面肯定要线程里面; 要否则python表明器会卡死.

固然 你也可以这么干;用C#界面类库打包写成DLL在用python调用. python只管传参数.
对于性能要求不高的场所就很合适.

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我可以不吃啊

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表