简体   繁体   English

从vb.net执行python脚本

[英]execute python script from vb.net

How I could execute a python script from within the VB.NET code? 如何在VB.NET代码中执行python脚本?

I would like to use VB.NET for building a GUI for my python script. 我想使用VB.NET为我的python脚本构建GUI。 This is to be able to create a GUI for the user of a script. 这是为了能够为脚本用户创建GUI。

I found this example: 我发现了这个例子:

Shell(c:\Program Files\python 266\python.exe path\myPythonScript.py +
       " " + arg1 + " " + argN)

But I would like to know is there a more suitable way of executing python script? 但是我想知道是否还有一种更合适的执行python脚本的方法?

I would also like to understand how I could handle errors in such setup. 我还想了解如何处理此类设置中的错误。

Your help much appreciated! 非常感谢您的帮助!

Thanks, 谢谢,

meryloo meryloo

This is how I did it in C# .Net: 这就是我在C#.Net中所做的事情:

    protected override void OnClick()
    {
        System.Diagnostics.Process.Start("C:\\path\\to\\pyscript\\MyScriptHere.py");
    }

在VB中,您还可以使用以下命令:

 shell( "d:\path\to\your\script.py arg1 arg2" )

You could call python from within .NET but if the only reason you want to do that is because you want to design a GUI fro your Python, why not keep things 'cleaner' and do it all in Python? 您可以从.NET中调用python,但是如果您要这样做的唯一原因是因为您想为Python设计GUI,那么为什么不让它保持“干净”并在Python中完成呢? You can use TKInter or wxPython to name but two GUI APIs. 您可以使用TKInterwxPython来命名,但只能使用两个GUI API。 The latter also has wxDesign which can lighten the load of GUI design. 后者还具有wxDesign,可以减轻GUI设计的负担。

This list gives you some more options for GUI design in Python other than the two I've listed. 除了上面列出的两个选项, 此列表为您提供了Python中GUI设计的更多选项。

On the other hand, you don't say what you are developing that needs python, but if it is either for ArcGIS or GDAL (probably the two most common GIS scripting environments) both of those have .NET APIs as well as Python. 另一方面,您没有说正在开发什么需要python,但是如果它是用于ArcGIS或GDAL(可能是两个最常见的GIS脚本环境),则它们都具有.NET API和Python。 So, you could potentially do it ALL in .NET. 因此,您可以在.NET中全部完成此操作。

In my case, I wanted the Python script to create a csv file, then the VB macro to gather info from that csv file. 就我而言,我希望Python脚本创建一个csv文件,然后是VB宏以从该csv文件中收集信息。 This code worked when I added a delay to the macro: 当我向宏添加延迟时,此代码有效:

Shell ("C:\Python27\ArcGIS10.1\python.exe myPath\PythonScript.py arg1 arg2")
FourSecDly

The delay allowed the shell process to complete before running the macro. 该延迟使Shell进程在运行宏之前完成。 The code for the delay was: 延迟的代码为:

Public Function FourSecDly()
y = Timer + 4#
Do While Timer < y
DoEvents
Loop
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM