【问题标题】:How to run an external script using visual basic?如何使用 Visual Basic 运行外部脚本?
【发布时间】:2018-05-01 17:40:02
【问题描述】:

我想使用 Visual Basic 和 Visual Studio 2017 制作一个 Windows 应用程序。 在这里,我想运行一个外部 Python 脚本,该脚本将在 Windows 窗体中按下“按钮 1”时运行。它将从表单中的文本框获取输入并进行一些计算,然后返回结果并将其放回表单中的文本框。

python 代码可以是这样的:

r = input()
p = 2*3.14*r
print(p) #Instead of print(p) something that can return the value to the form

或者这个:

def peri(r):
    p = 2*3.14*r
    return p

【问题讨论】:

  • 我不太熟悉,不过你可以看看铁蟒

标签: vb.net python-3.x visual-studio


【解决方案1】:

我设法为您找到了这个解决方案,我真的希望它有效:

1.Write your python code , for example : print("Hello world python !!") in text file and save to .py
2.Create project at VB.NET, drag component 1 button and 1 textbox into form.
3.Double click button1, at event click button1 write this code 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim proc As Process = New Process
        proc.StartInfo.FileName = "C:\Python34\python.exe" 'Default Python Installation
        proc.StartInfo.Arguments = pathmypythonfile.py
        proc.StartInfo.UseShellExecute = False 'required for redirect.
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 'don't show commandprompt.
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.RedirectStandardOutput = True 'captures output from commandprompt.
        proc.Start()
        AddHandler proc.OutputDataReceived, AddressOf proccess_OutputDataReceived
        proc.BeginOutputReadLine()
        proc.WaitForExit()

        TextBox1.Text = Value
    End Sub

    Public sub proccess_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        On Error Resume Next
        If e.Data = "" Then
        Else
            Value = e.Data
        End If
    End sub 

4. Create module file, and write this variable global :
    Module module
           Public Value As String
    End Module

5. Running the application, if textbox1 have populated with some string then your code was success.

HERE 是一个完整的过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 2011-05-26
    • 2016-06-07
    相关资源
    最近更新 更多