from tkinter import tk from pipe_gui import redirectedguishellcmd root tk(redirectedguishellcmd('python - spams py'rootfigure - command pipe gui displaying another program' output if the spawned program exitsexample - ' producer thread detects end-of-file on the pipe and puts final empty line in the queuein response the consumer loop displays an line in the gui by default when it detects this condition in this caseprogram exit is normal and silentin other caseswe may need to add shutdown logic to suppress error messages note that here againthe sleep call in the spawned program simulates long-running taskand we really need the - unbuffered streams flag--without itno output appears in the gui for eight secondsuntil the spawned program is completely finished with itthe gui receives and displays each line as it is printedone every two seconds this is alsofinallythe sort of code you could use to display the output of non-gui program in guiwithout socketschanges in the original programor blocking the gui of coursein many casesif you have to work this hard to add gui anyhowyou might as well just make your script traditional gui program with main window and event loop furthermorethe guis we've coded in this section are limited to displaying another program' outputsometimes the gui may have to do more for many programsthoughthe general separation of display and program logic provided by the spawned gui model can be an advantage--it' easier to understand both parts if they are not mixed together we'll learn more about sockets in the next part of the bookso you should consider parts of this discussion something of preview as we'll seethings start to become more and more interesting when we start combining guisthreadsand network sockets before we dothoughthe next rounds out the purely gui part of this book by applying the widgets and techniques we've learned in more realistically scaled programs and before thatthe next section wraps up here with preview of some of the more ways to add guis to non-gui code