1

I want to make a Windows application using visual basic and visual studio 2017. Here I want to run an external Python script which will run when 'Button 1' is pressed in the windows form. It will take input from the Textbox in the form and do some calculations and then return the result and put it back in the Textbox in the form.

The form is like this:

The python code can be either this :

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

OR This:

def peri(r):
    p = 2*3.14*r
    return p
1
  • I'm not too familiar with it, but you might look into iron python Commented May 1, 2018 at 17:43

2 Answers 2

2

I managed to find this solution for you and i really hope it works:

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 is a lick with the full process.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh boy, does this answer need some formatting help... but the edit queue is full?
0

This is the answer by @Dimitar_Georgiev, with small corrections and formatting fixes.

I managed to find this solution for you and i really hope it works:

  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 = "C:\path\to\my\pythonfile.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
  1. Create module file, and write this variable global :
Module module
       Public Value As String
End Module
  1. Running the application, if textbox1 have populated with some string then your code was success.

HERE is a lick with the full process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.