-1

I want my display from the console to be displayed in a GUI (Tkinter). It should display exactly when it outputs to the Python console and not after the project is finished. Can you do this with Tkinter or are there other alternatives? These are my current output. These should be shown on a gui in real Time.

Start program
iterations: [159]
Iteration 1 = complete
Iteration 2 = complete
.....
Iteration 159 = complete
lr 1.0
rc 1.0
rf 0.9966666666666667
gb 1.0
Training time: 8.76517425537s
Training finished
Process finished with exit code 0
Mayama
  • 49
  • 1
  • 8
  • Can you provide a program you already have to use as a basis? – Manby Jun 30 '21 at 16:39
  • Does this answer your question? [How to redirect print statements to Tkinter text widget](https://stackoverflow.com/questions/12351786/how-to-redirect-print-statements-to-tkinter-text-widget) – TheLizzard Jun 30 '21 at 16:40
  • @Manby The output I am showing here is all done with Print(..).`print("Start program"; print("iterations:", log_reg.n_iter_); print("iteration: {count} = complete"))`, etc. – Mayama Jun 30 '21 at 16:46
  • @TheLizzard - Thx it helped me. Ive created these `def redirector(inputStr): textbox.insert(INSERT, inputStr) sys.stdout.write = redirector` and a button which run the function. Its working. – Mayama Jun 30 '21 at 16:53

1 Answers1

2

You can do the following:

import sys
from tkinter import Tk, Button, Frame
from tkinter.scrolledtext import ScrolledText


class PrintLogger(object):  # create file like object

    def __init__(self, textbox):  # pass reference to text widget
        self.textbox = textbox  # keep ref

    def write(self, text):
        self.textbox.configure(state="normal")  # make field editable
        self.textbox.insert("end", text)  # write text to textbox
        self.textbox.see("end")  # scroll to end
        self.textbox.configure(state="disabled")  # make field readonly

    def flush(self):  # needed for file like object
        pass


class MainGUI(Tk):

    def __init__(self):
        Tk.__init__(self)
        self.root = Frame(self)
        self.root.pack()
        self.redirect_button = Button(self.root, text="Redirect console to widget", command=self.redirect_logging)
        self.redirect_button.pack()
        self.redirect_button = Button(self.root, text="Redirect console reset", command=self.reset_logging)
        self.redirect_button.pack()
        self.test_button = Button(self.root, text="Test Print", command=self.test_print)
        self.test_button.pack()
        self.log_widget = ScrolledText(self.root, height=4, width=120, font=("consolas", "8", "normal"))
        self.log_widget.pack()

    def reset_logging(self):
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

    def test_print(self):
        print("Am i working?")

    def redirect_logging(self):
        logger = PrintLogger(self.log_widget)
        sys.stdout = logger
        sys.stderr = logger


if __name__ == "__main__":
    app = MainGUI()
    app.mainloop()

mnikley
  • 1,625
  • 1
  • 8
  • 21
  • Thx. ive got it. But my problem is, that these are not a live output. Its just print it out if the Function is finished. – Mayama Jun 30 '21 at 19:06