[英]How can i pass values from a C# form into Python
I am using Visual C# as UI and Python in the background. 我在后台使用Visual C#作为UI和Python。
Is this possible? 这可能吗?
You could start your python in a new process in the background and pass the form items as arguments in the process start information as if you were running the python script from the command line, like so: 您可以在后台的新进程中启动python,然后将表单项作为参数传递给进程启动信息,就好像您是从命令行运行python脚本一样,如下所示:
var start = new ProcessStartInfo
{
FileName = _pathToPythonExecutable,
Arguments = string.Format(" {0} --arg1 {1}",_pathToYourPythonScript, //formItemValue),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
WorkingDirectory = _currentWorkingDirectory
};
using (Process process = Process.Start(start))
{
// Do stuff
}
You'll see that in my start information, I have told the process to Redirect StandardInput, Standard Output and Standard Error. 您会在开始信息中看到,我已经告诉过程重定向StandardInput,Standard Output和Standard Error。 This is another way that you can pass data between the processes.
这是在进程之间传递数据的另一种方法。
You would write to standard input like so: 您将这样写到标准输入:
process.StandardInput.Write(input);
process.StandardInput.Close();
Standard output and Standard Errors are streams, so you can read them like so 标准输出和标准错误是流,因此您可以像这样读取它们
// This would be the same for standard error
using (StreamReader reader = process.StandardOutput)
{
result = reader.ReadToEnd();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.