Python Forum
[Tkinter] Redirecting stdout to TextBox in realtime
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Redirecting stdout to TextBox in realtime
#1
I've managed to use multiprocessing to print the stdout to file and my terminal
but i'm lost when trying to redirect the stdout to the text box in realtime.
i'm seeing double outputs (i know, the print() does that, i want to redirect it to the textbox)

Processes:
[Image: 0kzQd.jpg]

what do I need to add to this block in order for it to work?

@classmethod
def generate_clr_yes_fn_no_print_output_to_gui(self, host):

    print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
    cmd = ["ping", "-n", "4", f'{host}']
    proc = sub.Popen(cmd, stdout=sub.PIPE, shell=True, stderr=sub.STDOUT)

    for stdout_line in proc.stdout:
        print(stdout_line.decode(), end='')
        yield stdout_line

    proc.stdout.close()
    return_code = proc.wait()
    if return_code:
        raise sub.CalledProcessError(return_code, cmd)
class code:

class TestPing(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent, bg="skyblue1")
        self.controller = controller

        self.clear_file = tk.BooleanVar()
        self.clear_file.set(False)

        self.url_label = tk.Label(self, text="Enter URL : ", padx=7, pady=5, bg="skyblue1")
        self.url_input_box = tk.Entry(self)

        self.file_name_label = tk.Label(self, text="Enter Filename: ", bg="skyblue1")
        self.file_name_input_box = tk.Entry(self)

        self.clear_file_label = tk.Label(self, text="Clear File?", padx=5, pady=5, bg="skyblue1")

        self.clear_file_radio_yes = tk.Radiobutton(self, text="yes", value=True, var=self.clear_file, bg="skyblue1",
                                           command=lambda: self.callback(self.clear_file.get()))
        self.clear_file_radio_no = tk.Radiobutton(self, text="no", value=False, var=self.clear_file, bg="skyblue1",
                                          command=lambda: self.callback(self.clear_file.get()))
        self.submit_button = tk.Button(self, text="Submit", width=10, height=1,
                                    command=lambda: self.condition(self.url_input_box.get(),
                                                             self.clear_file.get(), self.file_name_input_box.get()))
        self.clear_fields_button = tk.Button(self, text="Clear Fields", width=10,
                                      command=lambda: self.clear_boxes(self.url_input_box, self.file_name_input_box))

        self.c = tk.Canvas(self, height=300, width=500, bg="gray")


        self.grid_columnconfigure(0, minsize=50, weight=0)
        self.grid_columnconfigure(1, minsize=20, weight=2)
        self.grid_columnconfigure(2, minsize=50, weight=3)
        self.grid_columnconfigure(3, minsize=20, weight=2)
        self.grid_rowconfigure(0, minsize=50, weight=0)
        self.grid_rowconfigure(1, minsize=20, weight=0)
        self.grid_rowconfigure(2, minsize=30, weight=0)
        self.grid_rowconfigure(3, minsize=10, weight=0)
        self.grid_rowconfigure(4, minsize=10, weight=0)
        self.grid_rowconfigure(5, minsize=10, weight=0)

        self.url_label.grid(row=0, column=0, sticky="w")
        self.url_input_box.grid(row=0, column=1, sticky="w")
        self.file_name_label.grid(row=1, column=0, sticky="w")
        self.file_name_input_box.grid(row=1, column=1, sticky="w")
        self.clear_file_label.grid(row=0, column=1, sticky="n")
        self.clear_file_radio_yes.grid(row=1, column=1)
        self.clear_file_radio_no.grid(row=2, column=1)
        self.submit_button.grid(row=3, column=1, sticky="se")
        self.clear_fields_button.grid(row=3, column=1, sticky="sw")
        self.c.grid(row=7, column=1, sticky="swe")

    @classmethod
    def clear_boxes(self, urlInputBox, fileNameInputBox):
        urlInputBox.delete(0, "end")
        fileNameInputBox.delete(0, "end")

    @classmethod
    def callback(self, clearFile):
        print(f'Clear file = {clearFile}')  # Debugging Mode - check Radio box Var.

    def condition(self, host, clearFile, filenameInputBox):

        print(clearFile, filenameInputBox)  # Debugging - Input Validation
        if clearFile is True and filenameInputBox == '':
            self.handler_clr_yes_fn_no(host)
        elif clearFile is False and filenameInputBox == '':
            self.handler_clr_no_fn_no(host)
        elif clearFile is True and filenameInputBox != '':
            self.handler_clr_yes_fn_yes(host, filenameInputBox)
        elif clearFile is False and filenameInputBox != '':
            self.handler_clr_no_fn_yes(host, filenameInputBox)

    def handler_clr_yes_fn_no(self, host):

        startprocs = []
        lastprocs = []

        proc1 = mp.Process(name="Clear + No Filename + WriteFile",
                           target=self.clr_yes_fn_no_writefile, args=(host,))
        proc2 = mp.Process(name="Clear + No Filename + PrintOutput",
                          target=self.clr_yes_fn_no_print_output, args=(host,))
        proc3 = mp.Process(name="Clear + No Filename + Generate PrintOutput to GUI",
                           target=self.generate_clr_yes_fn_no_print_output_to_gui, args=(host,))
        proc4 = mp.Process(name="Remove first line + Write new default file",
                           target=self.delete_default_lines)

        startprocs.append(proc1)
        startprocs.append(proc2)
        startprocs.append(proc3)

        lastprocs.append(proc4)

        for s in startprocs:
            s.start()

        for s2 in startprocs:
            s2.join()

        for l in lastprocs:
            l.start()

    def handler_clr_no_fn_no(self, host):

        procs = []
        nextprocs = []

        proc1 = mp.Process(name="Append to default file",
                           target=self.clr_no_fn_no_writefile, args=(host,))
        proc2 = mp.Process(name="Print Output", target=self.clr_no_fn_no_printoutput, args=(host,))

        procs.append(proc1)
        procs.append(proc2)

        for proc in procs:
            proc.start()
        for proc in procs:
            proc.join()

        for p in nextprocs:
            p.start()

    def handler_clr_yes_fn_yes(self, host, filenameInputBox):

        procs = []
        nextprocs = []

        proc1 = mp.Process(name="Clear file + userFilename + Write to file",
                           target=self.clr_yes_fn_yes_writefile, args=(host, filenameInputBox,))
        proc2 = mp.Process(name="Clear file + user filename + Print output",
                           target=self.clr_yes_fn_yes_printoutput, args=(host,))
        proc3 = mp.Process(name="Remove Empty Lines from user filename",
                           target=self.delete_userfile_lines, args=(filenameInputBox,))

        procs.append(proc1)
        procs.append(proc2)
        nextprocs.append(proc3)

        for proc in procs:
            proc.start()

        for p in procs:
            p.join()

        for np in nextprocs:
            np.start()

    def handler_clr_no_fn_yes(self, host, filenameInputBox):

        procs = []

        proc1 = mp.Process(name="Keep File + Userfilename + Append to Userfile",
                           target=self.clr_no_fn_yes_writefile, args=(host, filenameInputBox,))
        proc2 = mp.Process(name="Keep File + Userfilename + Print Output",
                           target=self.clr_no_fn_yes_printoutput, args=(host,))

        procs.append(proc1)
        procs.append(proc2)

        for p in procs:
            p.start()

        for p2 in procs:
            p2.join()

    @classmethod
    def delete_default_lines(cls):

        time.sleep(1.5)
        print(f'\nCurrent Proccess: {mp.current_process().name} + {mp.current_process().pid}')
        file = fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default-tmp.txt'
        newfile = fr'c:/users/{os.getlogin()}/Desktop/default.txt'

        with open(file, 'r') as inp, open(newfile, 'w+') as out:
            for line in inp:
                if not line.isspace():
                    out.write(line.lstrip())
                    out.write('')
            inp.close()
            out.close()
        os.remove(file)

    @classmethod
    def delete_userfile_lines(cls, filename):

        time.sleep(1.5)
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}')
        file = fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/{filename}-tmp.txt'
        newfile = fr'c:/users/{os.getlogin()}/Desktop/{filename}.txt'

        with open(file, 'r+') as inp, open(newfile, 'w+') as out:
            for line in inp:
                if not line.isspace():
                    out.write(line.lstrip())
                    out.write('')
            inp.close()
            out.close()
        os.remove(file)

    @classmethod
    def clr_yes_fn_no_print_output(self, host):

        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        with sub.Popen(["ping", "-n", "4", f'{host}'], stdout=sub.PIPE,
                       bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p:
            for line in p.stdout:
                print(line, end=' ')

    @classmethod
    def generate_clr_yes_fn_no_print_output_to_gui(self, host):

        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        cmd = ["ping", "-n", "4", f'{host}']
        proc = sub.Popen(cmd, stdout=sub.PIPE, shell=True, stderr=sub.STDOUT)

        for stdout_line in proc.stdout:
            print(stdout_line.decode(), end='')
            # yield stdout_line

        proc.stdout.close()
        return_code = proc.wait()
        if return_code:
            raise sub.CalledProcessError(return_code, cmd)

    @classmethod
    def clr_yes_fn_no_print_output_to_gui(self):
        tk.Frame.__init__(self, parent)
        self.textbox.insert(tk.END, proc.stdout.decode())

    @classmethod
    def clr_yes_fn_no_writefile(self, host):

        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        file = fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default-tmp.txt'
        ping = sub.Popen(["ping", "-n", '4', f'{host}'], stdout=sub.PIPE)
        with open(file, 'w+') as output:
            data = output.read()
            for line in ping.stdout.readlines():
                data += str(line.decode())
            ping.stdout.close()
            output.seek(0)
            output.write(data.lstrip())

    @classmethod
    def clr_no_fn_no_printoutput(self, host):

        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        with sub.Popen(["ping", "-n", '4', f'{host}'], stdout=sub.PIPE,
                       bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p:
            for line in p.stdout:
                print(line, end=' ')

    @classmethod
    def clr_no_fn_no_writefile(self, host):

        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        with open(fr'c:/users/{os.getlogin()}/Desktop/default.txt', 'a') as output:
            sub.call(["ping", "-n", '4', f'{host}'], stdout=output)

    @classmethod
    def clr_yes_fn_yes_printoutput(self, host):

        with sub.Popen(["ping", "-n", '4', f'{host}'], stdout=sub.PIPE,
                       bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p:
            for line in p.stdout:
                print(line, end=' ')

    @classmethod
    def clr_yes_fn_yes_writefile(self, host, filename):

        file = fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/{filename}-tmp.txt'
        with open(file, 'w') as output:
            sub.call(["ping", "-n", '4', f'{host}'], stdout=output)

    @classmethod
    def clr_no_fn_yes_printoutput(self, host):

        with sub.Popen(["ping", "-n", '4', f'{host}'], stdout=sub.PIPE,
                       bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p:
            for line in p.stdout:
                print(line, end=' ')

    @classmethod
    def clr_no_fn_yes_writefile(self, host, filename):

        with open(fr'c:/users/{os.getlogin()}/Desktop/{filename}.txt', 'a') as output:
            sub.call(["ping", "-n", '4', f'{host}'], stdout=output)
TextBox:
[Image: trSgs.jpg]
Reply
#2
It is difficult for me to go into the details of this program which launches many subprocesses but I have some experience in sending other program's output to a tkinter text widget. The main idea that I implemented is that the tkinter program is going to use the widgets after() method to poll periodically an incoming source of data and write these data to the text window.

There are many ways to create this source of data. It could be a queue with some other thread or program at the other end. Sometimes I started a thread in the tkinter program to fill the queue by listening to sockets where other programs would connect and send data, but if you're launching other programs and you're able to read their output directly, it would work as well.
Reply
#3
Since you are already writing to a file, it is easy to open that file and write the contents to your textbox. As Gribouillis mentions, use after() to periodically call a function that reads from the file and writes to the display.

If the file is a stopgap measure and you would rather redirect stdout, use redirect_stdout from the contextlib module.
from contextlib import redirect_stdout, redirect_stderr
…
class Console(QtWidgets.QWidget):
    """A GUI version of code.InteractiveConsole."""
...
    def errorwrite(self, line):
        """Buffer stdout until you get a newline"""
        self.stdoutbuffer += line
        if self.stdoutbuffer [-1] == '\n':
            # Add line to display
            self.writeoutput(self.stdoutbuffer[:-1])
            self.stdoutbuffer = ''

    def writeoutput(self, line):
        """Display line in outdisplay"""
        self.outdisplay.moveCursor(QtGui.QTextCursor.End)
        self.outdisplay.appendPlainText(line)
        self.outdisplay.moveCursor(QtGui.QTextCursor.End)
...
    # Redirect stdout to console.write
    with redirect_stdout(console):
        console.show() # draws the window
        sys.exit(app.exec_()) # Like mainloop in tk
This is from a embeddable console I use in Qt applications. Most of it should be applicable to Tk other than the mechanics of how the text is appended to the text widget (I use QPlainTextEdit widget to display stdout).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  recording textbox data into a variable paul18fr 4 359 Feb-19-2024, 09:30 PM
Last Post: Axel_Erfurt
  [Tkinter] TkInter Realtime calculation marlonsmachado 3 1,086 Oct-28-2022, 02:02 AM
Last Post: deanhystad
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,599 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  [Tkinter] Tkinter Textbox only showing one character Xylianth 1 2,141 Jan-29-2021, 02:59 AM
Last Post: Xylianth
  tkinter how to unselect text in textbox Battant 0 2,223 Feb-17-2020, 02:00 PM
Last Post: Battant
  [Tkinter] cannot type in textbox msteffes 2 2,949 Jul-28-2018, 04:20 AM
Last Post: msteffes
  GUI insert textbox value into a Class dimvord 0 2,221 Jul-04-2018, 06:49 PM
Last Post: dimvord

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020