Redirect command line results to a tkinter GUI
You could create a script wrapper that runs your command line program as a sub process, then add the output to something like a text widget.
from tkinter import *
import subprocess as sub
p = sub.Popen('./script',stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()
root = Tk()
text = Text(root)
text.pack()
text.insert(END, output)
root.mainloop()
where script is your program. You can obviously print the errors in a different colour, or something like that.
TKinter redirect terminal output
You can override sys.stdout
:
class WriteToWindow():
def write(self, text):
... # write text to your window
def flush(self):
pass # this method should exist, but doesn't need to do anything
import sys
sys.stdout = WriteToWindow()
Now everything written to stdout, which includes calls to print
, will be sent to your WriteToWindow
class.
How can I displaymy console output in TKinter?
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()
Related Topics
How to Allocate Array With Shape and Data Type
Check If Value from One Dataframe Exists in Another Dataframe
Counting Non Zero Values in Each Column of a Dataframe in Python
How to Make a Roll the Dice Command With My Discord Bot
How to Find Rows of One Dataframe in Another Dataframe
How to Extract List from List of Lists When Any One Element Match With Another List'S Element
Splitting a Phone Number into a List of Digits: Python
Can Anyone Explain Me What This Python 3 Command Do
Python Ttk Treeview: How to Select and Set Focus on a Row
Convert Images from [-1; 1] to [0; 255]
Python Xlrd Unsupported Format, or Corrupt File.
How to Find the Average Colour of an Image in Python With Opencv
Add Numpy Array as Column to Pandas Data Frame
Filenotfounderror: [Errno 2] No Such File or Directory
Convert CSV to Parquet File Using Python
How to Sum Multiple Columns in a Spark Dataframe in Pyspark