I would like to display my program's "live" output on GUI (all what printed in it). how can i access to my output? and what the right way to display it for example in text box?
edited: where am i wrong? (I would like that the "hello world" to appear inside the text box. (Test2 is the running program))
from tkinter import *
from subprocess import *
print("Hello world")
def func():
proc = Popen("Test2.py", stdout=PIPE, shell=True)
proc = proc.communicate()
output.insert(END, proc)
Master = Tk()
Check = Button(Master, text="Display output", command=func)
Quit = Button(Master, text="Exit", fg="red", command=Master.quit)
output = Text(Master, width=40, height=8)
Check.pack(padx=20, pady=8)
Quit.pack(padx=20, pady=18)
output.pack()
Master.mainloop()
I took the time to debug and modify the
errorwindow.py
module in my answer to another question so it will work in both Python 2 and 3—the code in the linked answer was written for Python 2.x. Note I only did the minimum necessary to get it functioning under the two versions. The modified version of the script has been namederrorwindow3k.py
(despite that fact it also works in Python 2).The majority of the issues were simply due to module renaming, however there was a harder one to figure-out that turned-out was due to the switch to Unicode strings being the default string-type in version 3—apparently (on Windows anyway), pipes between processes are byte-streams, not Unicode characters. Fortunately the "fix" of decoding and then encoding the data on the other side also doesn't hurt in Python 2 which made correcting the problem fairly easy.
This nice thing is that using it is very easy. Just
import
it and from that point on any output sent to eithersys.stderr
orsys.stdout
will causetkinter
-based output windows to appear as needed to display the information. In your sample code just insertimport errorwindow3k
somewhere before theprint("Hello world")
.File
errorwindow3k.py
:If you have any questions about how it works, feel free to ask in the comments.