重定向Python的标准输入/输出到C#窗体应用程序 [英] Redirect Python standard input/output to C# forms application

查看:779
本文介绍了重定向Python的标准输入/输出到C#窗体应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我道歉,如果这是一个重复的问题,我搜索了一下,但没有找到任何类似 - 我有一个通过插座连接到我的C#应用​​程序,以便让简单的Python脚本一个Python库(IronPython的不现在一对夫妇的原因,一个选项)。我想创建一个Windows窗体控件,这将是一个基本图形前端的Python解释器,让用户可以不必拥有一个单独的控制台窗口中打开运行解释。



我重视的是我已经试过这么远低于一个简单的演示,但我一直没能得到它的工作。该DataReceived事件检索处理程序不会被调用,当我尝试写标准输入没有在解释情况。 ?有没有人有我做错了任何反馈意见,或者如果这甚至有可能。



 公共部分Form1类:表格
{

流程_pythonProc;

公共Form1中()
{
的InitializeComponent();
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
的ProcessStartInfo PSI =新的ProcessStartInfo()
{
的FileName = @C:\Python26\Python.exe,
CreateNoWindow = TRUE,
UseShellExecute =假,
RedirectStandardInput = TRUE,
RedirectStandardOutput = TRUE,
RedirectStandardError = TRUE
};
_pythonProc =新工艺();
_pythonProc.OutputDataReceived + = OutputDataReceived;
_pythonProc.ErrorDataReceived + = ErrorDataReceived;
_pythonProc.StartInfo =磅;
_pythonProc.Start();
}

私人无效cmdExecute_Click(对象发件人,EventArgs五)
{
串CMD = textInput.Text;
_pythonProc.StandardInput.WriteLine(CMD);
_pythonProc.StandardInput.Flush();
textInput.Text =的String.Empty;
}

私人无效Form1_FormClosed(对象发件人,FormClosedEventArgs E)
{
如果
_pythonProc.Kill()(_pythonProc.HasExited!);
}

私人无效OutputDataReceived(对象发件人,DataReceivedEventArgs参数)
{
textOutput.Text + = args.Data;
}

私人无效ErrorDataReceived(对象发件人,DataReceivedEventArgs参数)
{
textOutput.Text + = args.Data;
}

}


解决方案

在整个这种情况下,任何人都跌倒,我想通了这个问题 - 在默认情况下,如果它检测到,如果在程序运行的TTY设备连接到标准输入(这通常是唯一真正的Python解释器唯一进入交互模式从控制台)。为了重定向标准IO流,你必须设置UseShellExecute中的ProcessStartInfo为false,这将导致解释认为没有TTY连接,这意味着它会立即退出,因为它没有任何关系。



解决方案是运行Python解释器的-i命令行参数,这迫使解释器交互模式,无论是否有连接到标准一个TTY,这使得上面的例子正常工作。


I apologize if this is a duplicate question, I searched a bit and couldn't find anything similar - I have a Python library that connects to my C# application via a socket in order to allow simple Python scripting (IronPython isn't an option right now for a couple of reasons). I would like to create a Windows Forms control that would be basically a graphical front-end for the Python interpreter, so that the user could run the interpreter without having to have a separate console window open.

I attached a simple demo of what I've tried so far below, but I haven't been able to get it to work. The DataReceived event handlers are never called, and when I try to write to the standard input nothing happens in the interpreter. Does anyone have any feedback about what I'm doing wrong, or if this is even possible?

public partial class Form1 : Form
{

    Process _pythonProc;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ProcessStartInfo psi = new ProcessStartInfo()
        {
            FileName = @"C:\Python26\Python.exe",
            CreateNoWindow = true,
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true
        };
        _pythonProc = new Process();
        _pythonProc.OutputDataReceived += OutputDataReceived;
        _pythonProc.ErrorDataReceived += ErrorDataReceived;
        _pythonProc.StartInfo = psi;
        _pythonProc.Start();
    }

    private void cmdExecute_Click(object sender, EventArgs e)
    {
        string cmd = textInput.Text;
        _pythonProc.StandardInput.WriteLine(cmd);
        _pythonProc.StandardInput.Flush();
        textInput.Text = string.Empty;
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (!_pythonProc.HasExited)
            _pythonProc.Kill();
    }

    private void OutputDataReceived(object sender, DataReceivedEventArgs args)
    {
        textOutput.Text += args.Data;
    }

    private void ErrorDataReceived(object sender, DataReceivedEventArgs args)
    {
        textOutput.Text += args.Data;
    }

}

解决方案

In case anyone else stumbles across this, I figured out the problem - by default, the Python interpreter only enters interactive mode if it detects that a TTY device is connected to standard input (which is normally only true if the program is run from the console). In order to redirect the standard IO streams, you have to set UseShellExecute in the ProcessStartInfo to false, which causes the interpreter to think that there is no TTY connected, meaning it immediately exits since it has nothing to do.

The solution is to run the Python interpreter with the "-i" command line argument, which forces the interpreter to interactive mode, regardless of whether there is a TTY connected to standard in. This makes the example above work correctly.

这篇关于重定向Python的标准输入/输出到C#窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆