G. Trialonis
G. Trialonis

Reputation: 99

How can I print more than one line of words/phrases in a Multiline PySimpleGUI element?

The following small program creates a window with a Multiline element, an Input element and a button. When I type a word or phrase in the Input field, the word/phrase also prints inside the Multiline element. But when I type a second word/phrase the first one is replaced by the second in the Multiline. How can I keep the two or more of them, one below the other (in multiple lines) within the Multiline element? Here is the code:

import PySimpleGUI as sg

sz = (7,1)

column1 = [ [sg.ReadButton('UPDATE\nTEXT', size=sz)],
            [sg.ReadButton('LOAD-\nTEXT', size=sz)],
            [sg.ReadButton('SAVE-VOC', size=sz)],
            [sg.ReadButton('CLEAR\nTEXT', size=sz)]
            ]

column2 = [[sg.ReadButton('Click here')]]

col_layout = [
                [sg.InputText(focus=True, key='word')],
                [sg.Column(column2)]
             ]

layout = [
            [sg.Text("Study Text Box")],
            [sg.Multiline(size=(50,10), font='Tahoma 13', key='-STLINE-', autoscroll=True), sg.Column(column1), sg.VerticalSeparator(pad=None), sg.Column(col_layout)]
         ]
        

window = sg.Window("TbLLT Program", layout, resizable=True, finalize=True)

while True:
    event, values=window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'SAVE-VOC':
        with open('someText(saved).txt', 'w+') as file:
            savedText1 = file.write(values['-STLINE-'])
        file.close()
    if event == 'Click here':
        window['-STLINE-'].update(values['word'])
window.close()

Multiline window with Input field

Upvotes: 4

Views: 6990

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

Use method update will replace full content of sg.Multiline, or one more option append=True, but you need to add '\n' by yourself when needed.

Use method print of sg.Multiline to print like Python normally prints except route the output to a multiline element and also add colors if desired.

def print(self, *args, end=None, sep=None, text_color=None,
    background_color=None, justification=None, font=None, colors=None, t=None,
    b=None, c=None,  autoscroll=True):

Call it like

        window['-STLINE-'].print(values['word'])

or call method update with option append=True.

        window['-STLINE-'].update(values['word']+'\n', append=True)

Upvotes: 3

Related Questions