How to have PySimpleGUI default text in Output on window opening

Viewed 1868

I am using PySimpleGUI to create a text output box.

On opening the program, I would like the Output to display some default text before any button is pressed.

How can I get this to occur? window.Read() waits for a button press. window.refresh() doesn't appear to force the text to the window.

import PySimpleGUI as sg

initialString = "I want this text to display on window opening."

def gui2():
    layout = [
              [sg.Output(size=(90,20), background_color='black', text_color='white')],
              [sg.Button('Do things'), sg.Button('Exit')]
             ]
    
    window = sg.Window("Funny Title", layout)
    
    #window.read()  #I need to press a button before the text will display
    #window.refresh() #doesn't refresh the output
    print(initialString)
    #window.refresh() #doesn't refresh the output
    
    while True:
        event, values = window.read() 
        if event in (sg.WIN_CLOSED, 'Exit'):
            break
        elif event == 'Do things':
            print("You pressed the button")
    window.close() 
gui2()
1 Answers

and of course, the answer is found 5 minutes after I post.

window = sg.Window("Funny Title", layout, finalize = True)

This fixes the window, and the subsequent print statement appears in the Output as wanted.

.

Edit: From the comment below, I've also changed the layout from sg.Outline to sg.Multiline:

              sg.Multiline(size=(90,20), 
                           background_color='black', 
                           text_color='white', 
                           reroute_stdout=True, 
                           reroute_stderr=True,
                           autoscroll = True)],
Related