Reputation: 33
I am hoping someone can help me.
Basically, I have an arbitrary script, in this script, there are many functions and after each function is executed, print() is executed as well to give me an update. I'm using the Pysimplegui library for GUI, and was wondering if someone can help or explain what I can do to show the print output in the GUI
Upvotes: 2
Views: 10159
Reputation: 81
you can utilize Multiline object and it's method Multiline.print
then just monkey patch it like this:
print = Multiline.print
ready window looks like this:
import PySimpleGUI as sg
logwindow = sg.Multiline(size=(70, 20), font=('Courier', 10))
print = logwindow.print
layout = [[logwindow]]
# Create the window
window = sg.Window("Demo", layout, finalize=True)
# Create an event loop
while True:
event, values = window.read(timeout=1)
for i in range(100):
print(f'print {i}')
if event == sg.WIN_CLOSED:
break
window.close()
Upvotes: 1
Reputation: 7541
There are a few ways to do this that are in the documentation, here is one way.
import PySimpleGUI as sg
# This is the normal print that comes with simple GUI
sg.Print('Re-routing the stdout', do_not_reroute_stdout=False)
# this is clobbering the print command, and replacing it with sg's Print()
print = sg.Print
# this will now output to the sg display.
print('This is a normal print that has been re-routed.')
Here is the example output
Upvotes: 4
Reputation: 47
Whenever you are going to use any library, first read the docs. I am sure you can find your answer. https://pysimplegui.readthedocs.io/en/latest/
Upvotes: -1