如何在 window 打开时在输出中使用 PySimpleGUI 默认文本

How to have PySimpleGUI default text in Output on window opening

我正在使用 PySimpleGUI 创建文本输出框。

打开程序时,我希望在按下任何按钮之前输出显示一些默认文本。

我怎样才能做到这一点? window.Read() 等待按下按钮。 window.refresh() 似乎没有将文本强制到 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()

当然,答案是在我 post 后 5 分钟找到的。

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

这修复了 window,随后的 print 语句将按需要出现在输出中。

.

编辑: 从下面的评论中,我还将布局从 sg.Outline 更改为 sg.Multiline:

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