Python tkinter displaying console output on GUI

Im looking for a way to put the console log on a GUI text/label fiel. i.e. everything that I have print command in my script that puts a message in the console.

I have found this script , which works for the netsat process but what I can’t find is the consolse process. How do I find this ? I’ve tried console, consolehost etc etc but doesnt’ work.

I am using spyder on windows

Alternatively if any one has better suggestions how to achieve this ?

import tkinter as tk
import subprocess
import threading

class CmdThread (threading.Thread):
   def __init__(self, command, textvar):
        threading.Thread.__init__(self)
        self.command = command
        self.textvar = textvar

   def run(self):
        proc = subprocess.Popen(self.command, stdout=subprocess.PIPE)
        while not proc.poll():
            data = proc.stdout.readline()
            if data:
                print(data)
                self.textvar.set(data)
            else:
                break

window = tk.Tk()
text = tk.StringVar()
label = tk.Label(textvariable=text)
label.pack()

thread = CmdThread(['netstat'], text)
thread.start()

window.mainloop()
1 Like