Change name of sg.Print window in PySimpleGUI
Question:
At the beginning of my python code I have print = sg.Print . So whenever print is used, it prints to a Window called Debug Window. According to documentation, I can close the window with sg.PrintClose() and that works, but I can’t change or set the name of the Debug Window.
I also tried the following to send to GUI output box:
def sg_Print(*args, **kwargs):
output_element = window['-OUTPUT-']
output_text = output_element.get() # get current text in element
output_element.update(value=output_text + 'n' + ' '.join(map(str, args)))
print = sg_Print
Thanks for any advice!
Answers:
If you create a window (debugWindow in the example) then you can set the title and all the other properties you would like. Then you redirected Stdout to it and use the normal print statements in python. Turn off redirecting when you are done.
Here is simple code:
import PySimpleGUI as sg
title = "Debug Window"
debugWindow = sg.Window(title, [
[sg.Multiline("This is for Output:n", k='--DEBUG--', size= (50,20))],
[sg.B("Close")]], finalize=True)
print("This will go to Stdout:")
debugWindow['--DEBUG--'].reroute_stdout_to_here()
print("This will goto the multi line box")
for i in range(25):
debugWindow.read(timeout=50)
if i == 15:
print("This is last to multi-line box")
debugWindow['--DEBUG--'].restore_stdout()
print(f"Message #{i}")
At the beginning of my python code I have print = sg.Print . So whenever print is used, it prints to a Window called Debug Window. According to documentation, I can close the window with sg.PrintClose() and that works, but I can’t change or set the name of the Debug Window.
I also tried the following to send to GUI output box:
def sg_Print(*args, **kwargs):
output_element = window['-OUTPUT-']
output_text = output_element.get() # get current text in element
output_element.update(value=output_text + 'n' + ' '.join(map(str, args)))
print = sg_Print
Thanks for any advice!
If you create a window (debugWindow in the example) then you can set the title and all the other properties you would like. Then you redirected Stdout to it and use the normal print statements in python. Turn off redirecting when you are done.
Here is simple code:
import PySimpleGUI as sg
title = "Debug Window"
debugWindow = sg.Window(title, [
[sg.Multiline("This is for Output:n", k='--DEBUG--', size= (50,20))],
[sg.B("Close")]], finalize=True)
print("This will go to Stdout:")
debugWindow['--DEBUG--'].reroute_stdout_to_here()
print("This will goto the multi line box")
for i in range(25):
debugWindow.read(timeout=50)
if i == 15:
print("This is last to multi-line box")
debugWindow['--DEBUG--'].restore_stdout()
print(f"Message #{i}")