Fully mimic stdout in tkinter text widget to support the end parameter from Python's print function

Viewed 162

How do I fully mimic sys.stdout in a tkinter text widget so that I can use the print() function (mainly the end parameter) to its full capacity? So that I can use print("Hello World", end='\r') to print to the text widget where if I print something else it will overwrite the previously printed text.

class Main:
    def __init__(self):
        self.master = tk.Tk()
        self.output = tk.Text(self.master)
        self.output.pack()
        sys.stdout = self
        self.master.mainloop()

    def write(self, txt):
        self.output.insert(tk.INSERT, str(txt))
        self.output.see('end')

    def flush(self):
        # What do I do here?

Running

print("Hello World", end='\r')
print("Goodbye World", end='\r')

Gives the following in the text widget

Hello World

Goodbye World

But I expect it to show the following instead

Goodbye World

As a bonus question, how would I (re)define the flush functionality of stdout to work with a tkinter text widget?

1 Answers

I fixed the problem with the return carriage by updating the write function with the following:

def write(self, txt):
    if '\r' in txt:
        self.return_carriage = True
        return
    if self.return_carriage:
        self.output.delete(tk.INSERT + " linestart", tk.INSERT + " lineend")
        self.return_carriage = False
    self.output.insert(tk.INSERT, str(txt))
    self.output.see('end')

This only solves the problem when using end='\r' in the print() function though. This still does not mimic the full sys.stdout.write functionality.

Related