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


Messages In This Thread
Redirecting stdout to TextBox in realtime - by Gilush - Jun-06-2020, 06:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  recording textbox data into a variable paul18fr 4 543 Feb-19-2024, 09:30 PM
Last Post: Axel_Erfurt
  [Tkinter] TkInter Realtime calculation marlonsmachado 3 1,194 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,715 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  [Tkinter] Tkinter Textbox only showing one character Xylianth 1 2,225 Jan-29-2021, 02:59 AM
Last Post: Xylianth
  tkinter how to unselect text in textbox Battant 0 2,307 Feb-17-2020, 02:00 PM
Last Post: Battant
  [Tkinter] cannot type in textbox msteffes 2 3,063 Jul-28-2018, 04:20 AM
Last Post: msteffes
  GUI insert textbox value into a Class dimvord 0 2,293 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