简体   繁体   中英

How to display python output in C# textbox?

I want to display standard output from python script in textbox, but after click the button nothing happend. If it's wrong how can I fix it or replace? Example I run my aplication in textbox1 i have nothing and after clicking button1 I want to have in my textbox1 'Hello' from python script

private void button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                char[] spliter = { '\r' };
                int x = 1;

                Process python = new Process();
                python.StartInfo.FileName = "D:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64\\python.exe";
                python.StartInfo.RedirectStandardOutput = true;
                python.StartInfo.UseShellExecute = false;

                python.StartInfo.Arguments = string.Concat("C:\\Users\\kamil\\source\\PythonApplication3\\PythonApplication3.py", " ", x.ToString());
                python.Start();

                StreamReader sReader = python.StandardOutput;
                string[] output = sReader.ReadToEnd().Split(spliter);


                foreach (string s in output)
                {
                    TextBox1.Text += s + Environment.NewLine;
                }

                python.WaitForExit();

Python script:

import sys

def main():
    print('Hello')

main()

Try adding python.StartInfo.RedirectStandardError = true; and textBox1.Text += python.StandardError.ReadToEnd(); to your code so that you can also capture the errors in addition to the output. I would reason that it may not be able to "find" the file you are passing as an argument. Try adding quotes around the file path like so: "\\"C:\\Users\\kamil\\source\\PythonApplication3\\PythonApplication3.py\\""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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