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.

picture

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!

Asked By: BioFind17

||

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}")
Answered By: RAllenAZ
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.