0

I have a Vb.net program that runs python script in the background. I want a get the python input questions input("Enter num"). I would like to get this so I can get users to input values before writing tothe python StandardInput

VB.NET

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Controls.Add(TextBox1)
        Dim p As Process = New Process
        p.StartInfo.FileName = "C:\Users\luqma\AppData\Local\Programs\Python\Python38-32\python.exe" 'Default Python Installation
        p.StartInfo.Arguments = "File.py"
        p.StartInfo.UseShellExecute = False 'required for redirect.
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 'don't show commandprompt.
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.RedirectStandardInput = True
        p.StartInfo.RedirectStandardOutput = True 'captures output from commandprompt.
        p.StartInfo.RedirectStandardError = True
        p.Start()
        Dim reader As StreamReader = p.StandardOutput
        Dim myStreamwrite As StreamWriter = p.StandardInput
        Dim onlyValue As Int32
        Dim part1 As String
        Dim part2 As String
        onlyValue = 1
        While Not p.StandardOutput.EndOfStream
            Dim output As String = reader.ReadLine

            If myStreamwrite.BaseStream.CanWrite Then
                myStreamwrite.WriteLine(1)
                TextBox1.Text += Environment.NewLine + ">>>" + output + Environment.NewLine
            Else
                TextBox1.Text += Environment.NewLine + ">>>" + output + Environment.NewLine
            End If
        End While

Python - I want to ask users what's in the bracket so they can input the values

b = int(input("Enter num:"))#RUNS WHEN THERE IS ONLY THIS INPUT

Luqmaan
  • 13
  • 6
  • Subscribe to `OutputDataReceived` event (and remove that loop, of course), show the text in a Control, use another control to receive the User response and write it to StdIn. See here: [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). Maybe download the test Form and see how it works, or just read the notes there (I think I've already written this comment, it looks like a deja-vue). – Jimi Oct 13 '20 at 22:30
  • I want to show users the "Enter num:" thats in the brackets but theres nothing to get specific text – Luqmaan Oct 13 '20 at 22:51
  • Did you take a look at the code I linked? You need a StreamWriter object, declared in a different scope than a Button.Click handler. You can use a Field, declared in the scope of your Form class. Use the `OutputDataReceived` handler to show a User what the Process outputs, in a TextBox or whatever, then use another TextBox to let Users input their response. When a User presses `Enter` (classic *I'm done writing*), you write the content of this TextBox using the StreamWriter that is attached to the Process' `StandardInput`. That's all. – Jimi Oct 13 '20 at 23:02
  • Ok Thanks. WIll try it out – Luqmaan Oct 13 '20 at 23:04
  • No luck, wasnt working – Luqmaan Oct 14 '20 at 00:19

0 Answers0