1

I'm making a simple GUI for an app that performs some slow calculations. I though about adding something similar to a console so I could display messages informing about the execution of program. Since I'm working with Tkinter, I've figure I would add a text box and redirect the standard output to it, which in principle was pretty neat. However, when I press the button that starts the calculations, the interface halts completely, and the text box is only updated when the calculations are finished, which defeats the purpose of this console.

Is there a way not to halt the window and actually update the text live?

Here is a minimal working example of the problem:

from tkinter import *
    import math, sys

class StdoutRedirector(object):
    def __init__(self, text_widget):
        self.text_space = text_widget

    def write(self, string):
        self.text_space.insert('end', string)
        self.text_space.see('end')

def slowprint():
    print("First line")
    aaa = math.factorial(300000)
    print("Second line")
    bbb = math.factorial(300000)
    print("Third line")

window = Tk()

text_box = Text(window, wrap='word')
text_box.grid(columnspan=5, row=1, sticky='nswe', padx=5, pady=5)
sys.stdout = StdoutRedirector(text_box)
Button(window,text='Print', command=slowprint).grid(padx=5, pady=5)

When the button is pressed, the window halts, then all messages are printed at once. It would be nice if it was possible to print each message when the print function is actually called.

Mefitico
  • 816
  • 1
  • 12
  • 41
  • @Aran-Fey time.sleep was a poor choice to make this example. The calculations are performed by a C program which prints to stdout. If I chose to use the `after` command, then I guess I would also need to breakdown the C program and run steps separately. This would be very much impractical. – Mefitico Mar 26 '19 at 19:42
  • 1
    Well, then you'll have to completely rewrite this question. 1) Your nice little `StdoutRedirector` will have absolutely no effect on your C program; 2) reading the output of an external program in real time isn't an easy thing to do in its own right; and 3) your GUI really shouldn't be freezing while the C program is running unless you did something silly. – Aran-Fey Mar 26 '19 at 19:48
  • @Aran-Fey: getting realtime output isn't particularly hard. – Bryan Oakley Mar 26 '19 at 19:50
  • 1
    See [Getting realtime output using subprocess](//stackoverflow.com/q/803265) – Aran-Fey Mar 26 '19 at 19:51
  • @Aran-Fey: Great hint regarding the subprocess approach, but there are also python modules with prints of their won that are part of this program, so if it was possible to solve the issue for the question with the factorial calculations, that would also be very helpful. – Mefitico Mar 26 '19 at 19:56
  • You'll have to run `slowprint` in a thread. [Tkinter: How to use threads to preventing main event loop from "freezing"](//stackoverflow.com/q/16745507) might help. – Aran-Fey Mar 26 '19 at 20:00

0 Answers0