Web Analytics Made Easy - Statcounter
Skip to content

Multiline

Multiline call reference

Input or output multiple lines of text. Can be used as an input, an output, or both.

layout = [[sg.Multiline(default_text='This\nelement\nhas\nmultiple\nlines!', size=(None, 3))]]

Events

When used as an input element, there are 2 sources for events that the Multiline can generate.

  1. enable_events is True - Any key press generates an event
  2. enter_submits is True - Pressing the return key generates an event

Values Dictionary

If the Multiline element is not in "write only mode", then the values dictionary will contain the current value shown in the element. There are 2 ways to put the Multiline in write-only mode. 1. Set parameter write_only to True 2. The string version of the element's key contains the constant sg.WRITE_ONLY_KEY

As an Input Element

If you've not indicated this element should be write-only, then an entry will be made in the values dictionary.

The parameter rstrip, which defaults to True, is important to take into consideration if you are using the Multiline as an input element. When rstrip is enabled, then all whitespace is stripped from the end of the input string.

If rstrip is False, then the Multiline value will be \n when nothing has been input. This was confusing users so the rstrip option was added.

Like the single-line Input element, you can use the parameter do_not_clear with the Multiline element to make creation of Windows that behave as data-entry forms.

Outputting to Multiline Element

In addition to the normal update method, there are numerous ways for you to make changes to a Multiline element including:

  • Call the Multiline.print method
  • Call cprint with specific key set to output to
  • Call cprint after rerouting cprint to the element
  • Reroute stdout/stderr to the element
  • Echo stdout/stderr to the element

Color, Font, Size

One of the most exciting features of outputting using cprint and print is that individual text color, background color, and font can be controlled on a character by character basis.

import PySimpleGUI as sg

layout = [[sg.Multiline(key='-ML-', write_only=True, size=(60,10), reroute_cprint=True)]]

window = sg.Window('Multiline Example', layout, finalize=True)

sg.cprint('Default font')
sg.cprint('Courier 12', font='Courier 12', colors='white on red', )
sg.cprint('Courier 20 bold', font='Courier 20 bold', colors='white on green', end='')
sg.cprint('default font', colors='white on red', end='')
sg.cprint('Courier 12', font='Courier 12', colors='white on blue')
sg.cprint('Default font size 12', font='_ 12')
sg.cprint('Default font')

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break

window.close()

image