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?