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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 1,668 Feb-19-2024, 09:30 PM
Last Post: Axel_Erfurt
  [Tkinter] TkInter Realtime calculation marlonsmachado 3 2,072 Oct-28-2022, 02:02 AM
Last Post: deanhystad
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 3,617 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  [Tkinter] Tkinter Textbox only showing one character Xylianth 1 2,993 Jan-29-2021, 02:59 AM
Last Post: Xylianth
  tkinter how to unselect text in textbox Battant 0 2,907 Feb-17-2020, 02:00 PM
Last Post: Battant
  [Tkinter] cannot type in textbox msteffes 2 3,917 Jul-28-2018, 04:20 AM
Last Post: msteffes
  GUI insert textbox value into a Class dimvord 0 2,814 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