Redirecting the command-line output to Tkinter

You may occasionally need to redirect the output of a command line to a GUI such as Tkinter. The ability to pass outputs from the command line to Tkinter opens a large pool of possibilities for using the inherent powers of the shell on Unix and Linux operating systems and the Windows shell on a Windows machine.

We will demonstrate this by using the subprocess Python module, which lets us spawn new processes, connect to the input, output, and error pipes of this new process, and obtain the return codes from the programs.

A detailed discussion on the subprocess module can be found at https://docs.python.org/3/library/subprocess.html.

We will use the Popen class from the subprocess module to create a new process.

The Popen class provides a cross-platform way to create new processes, and it has the following long signature to handle most of the common and esoteric use cases:

subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=())

Here's a simple program that shows how we can redirect the output of the ls Bash shell command to Tkinter's text widget. As a reminder, the ls command in the Bash scripting language returns a list of all files and directories (see the 10.11_reading_from_command_line.py code):

from tkinter import Tk, Text, END
from subprocess import Popen, PIPE
root = Tk()
text = Text(root)
text.pack()

#replace "ls" with "dir" in the next line on windows platform
with Popen(["ls"], stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
text.insert(END, line)

root.mainloop()

Windows users should note that you will have to replace ls with dir in the highlighted part of the preceding code to get an equivalent result.

Furthermore, note that you can pass extra arguments to Popen by using the following format:

Popen(['your command', arg0, arg1, ...])

Even better, you can pass the name of the script file that needs to be executed in the new process. The code used to run a script file is as follows: 

Popen('path/toexecutable/script',stdout=sub.PIPE,stderr=sub.PIPE)

However, the script file that needs to be executed must include a proper shebang declaration to let the program choose a proper executing environment for your script. For instance, if you intend to run a Python script, your script must begin with the shebang of the #!/usr/bin/env python3 form. Similarly, you need to include #!/bin/sh to run a Bourne-compatible shell script. A shebang isn't necessary on Windows. It is also not required for binary executables.

Running the preceding program produces a window, and a listing of all the files from the current directory are added to the text widget, as shown in the following screenshot:

While the preceding program is simple, this technique can have a lot of practical uses. For instance, you may recall that we built a chat server in the previous chapter. Every time a new client connected to the server, it printed the client details to the terminal. We could have easily redirected that output into a new Tkinter app. This would enable us to create a dashboard for the server; from there, we could have monitored all the incoming connections to the server.

This opens the door for us to reuse any command-line script written in any other programming language, such as Perl or Bash, and directly integrate it with a Tkinter program.

This concludes the brief section on the redirection of command-line outputs into Tkinter programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.225