为在python中使用dotnet程序安装clr

直接在命令行中录入

pip install pythonnet

会有一个ssl错误提示

...Could not fetch URL https://pypi.python.org/simple/pythonnet/: There was a problem confirming the ssl...

 只好下载后再安装,下载地址

https://pypi.org/simple/pythonnet/

我的python是2.7的32位,于是下载了

pythonnet-2.3.0-cp27-cp27m-win32.whl

直接放在D盘,在命令行转到D盘,执行以下命令

pip install pythonnet-2.3.0-cp27-cp27m-win32.whl

安装成功,可以import clr啦:)

 vs.net2017也可以新建python项目,我在其上安装的是python3.6,且是64位的,这很有意思,命令行默认是2.7的,vs.net里跑的是3.6的,太方便了。vs.net2017的3.6安装clr就太方便了。

录入组件名称,点击运行pip...

好了,帖一段python里打开wpf的程序的代码,代码来自这个地址

https://blog.csdn.net/soloman817/article/details/6788830

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.Threading import Thread
from System.Threading import ApartmentState
from System.Threading import ThreadStart 

def AppStartUp(sender, e):
    mainWnd = Window()
    mainWnd.Title = "WPF From PythonNet!"
    mainWnd.Show() 

def STAMain():
    app = Application()
    app.Startup += AppStartUp
    app.Run()

def main():
    t = Thread(ThreadStart(STAMain))
    t.ApartmentState = ApartmentState.STA
    t.Start()
    t.Join()

if __name__ == "__main__":
    main()
View Code

 

posted @ 2019-01-07 19:17  生命体验之kevin-Y  阅读(7068)  评论(0编辑  收藏  举报