Tkinter Displaying Terminal Output Tkinter Displaying Terminal Output tkinter tkinter

Tkinter Displaying Terminal Output


You can't exactly show the terminal in your program, but I don't think that's really what you want anyway. It sounds like you just want to display the output of the command in your GUI.

How are you running the calculation? Are you using popen? If so, you can grab the output of the program and insert it into the text widget with something like this:

p = sub.Popen('./script',stdout=sub.PIPE,stderr=sub.PIPE)output, errors = p.communicate()self.text.insert("end", output)

It gets a little more complicated if you're using threads or multiprocessing, but the concept is the same: run your program in a way that lets you capture the output, then insert the output in the text widget when it's done running.


Hmm, I'm definitely not an expert but let me give it a shot.Something you could do is to redirect all your terminal output to a file, say calculation.py <userinput> >> text.txt or have that in your calculation code i.e.:

while calculation: # <- your function     with open('text.txt', 'w') as f: # <- open a text file for ouput        f.write(<calculation output>) # <- keep writing the output

or if your program is not python than os.system('<terminal command i.e. "echo foo">') and use the first method to direct the ouput to a file.

After doing so, in your tkinter code:

def cmdOutput():    output = os.system('cat text.txt') # <- replace "cat" with "type" if windows.    return output.split("\n")[-1] # <- return the last item in the list.while cmdOutput() == "<calculating...>": # <- or something like that.    tkinterUpdateMethodDeclaredSomewhere(cmdOutput())else:    tkinterUpdateMethodFinalAnswer(cmdOutput())

Though I'm sure there's a better way to do this, this is one way...you could also write a command to delete the file at the end of cmdOutput, or read from it and paste the necessary bits to a log file or something...

I hope this helps.