1

I'm trying to launch a Python Tkinter app from C#. That app uses stdin and stdout beside its GUI. How can I launch it from a dotnet Core app so that I can access its stdin/stdout?

If I use UseShellExecute=false I can redirect the streams, but the GUI will not appear. If I use UseShellExecute=true the GUI appears, but the streams cannot be redirected.

I also tried starting the process with cmd /c py ... instead of just py .... Still, the GUI does not appear in UseShellExecute=false mode.

Update:

At this point it seems like the issue is somehow python related. The code below works, and the window appears at mainloop(), but only at mainloop(). This is NOT how the same app behaves if I run it directly from cmd. In that case the GUI already appears before the input is given. What can cause this difference in behavior?

var psi = new ProcessStartInfo
            {
                FileName = "py",
                Arguments = @"s:\dsgui\ex.py",
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false
            };
            var process = Process.Start(psi);
            process.StandardInput.WriteLine("test");

ex.py:

import tkinter
tkinter.Tk()
print(input())
tkinter.mainloop()
Ádám Bozzay
  • 529
  • 7
  • 19
  • Does this answer your question? [Redirecting standard input of console application](https://stackoverflow.com/questions/21848271/redirecting-standard-input-of-console-application) and [C# - Realtime console output redirection](https://stackoverflow.com/questions/4501511/c-sharp-realtime-console-output-redirection) and [Redirect input and output for cmd.exe](https://stackoverflow.com/questions/15870516/redirect-input-and-output-for-cmd-exe) –  Aug 03 '21 at 18:49
  • [How to redirect Standard Input/Output of an application](https://www.codeproject.com/Articles/18577/How-to-redirect-Standard-Input-Output-of-an-applic) • [StreamPipe.cs GIST](https://gist.github.com/antopor/5515bed636c3d99395ea) –  Aug 03 '21 at 18:49
  • 1
    Hmm... This may have to do something with synchronization and threads. If I remove the `RedirectStandardOutput` and `RedirectStandardInput` but leave the `UseShellExecute=false` the GUI comes up. But as soon as I add either of the redirection (even if I don't use the redirected stream) the GUI does not come up. At this point your suggested questions are not useful, because all of them require the RedirectStandard... to be true. – Ádám Bozzay Aug 03 '21 at 19:21
  • I created a simple example to reproduce the issue, and added it to the question. This will hopefully clarify my usecase. – Ádám Bozzay Aug 03 '21 at 19:51
  • I found more info about this strange behavior, I updated the description. – Ádám Bozzay Aug 03 '21 at 20:33

0 Answers0