Search code examples
pythonsavemultilinepysimplegui

PySimpleGui save Multiline as .txt


I want to save Multiline (PySimpleGui) as .txt.

[sg.Multiline(
              enter_submits=True,
               key='_LOG_'
))]

The program writes into it

window['_LOG_'].print('something')

and when it's done it should automatically save it as .txt.


Solution

  • In your event loop, values is the content of some elements before your actions for that event, so you cannot use the value of values['_LOG_'] to save your log file.

    Try use method of get of sg.Multiline to get current content of sg.Multiline element.

            window['_LOG_'].print("Hello World")
    
            with open("LogFile.txt", "wt", encoding='UTF-8') as f:
                f.write(window['_LOG_'].get())