I have a PySimpleGUI.Text() element that I’d like to update with text as my program executes. I want it to be five lines high. It will be contained in another element, so I do not want to set its width explicitly. Instead, I set expand_x = True
so that it fills its available space horizontally. Thus, to set its size, I use size=(None,5)
.
Having done this, I find that when I update the element’s text value, the text renders as one character of the text string per line, so that the updated text ends up running vertically. It is clear that the parameter size=(None,5)
is what’s causing the error. If I remove it, or if I set the width parameter to a large explicit number, the text updates normally. If I set the width to be a small explicit number, the “squeezed” behavior of one character per line returns.
The following code illustrates the problem:
import PySimpleGUI as sg
text_box = sg.Text('', key = '-TEXT-', relief = 'solid', border_width = 1,
# This line causes text to update one character per line:
size = (None, 5),
# So does this:
#size = (1, 5),
# This line causes text to update normally:
#size = (30, 5),
expand_x = True,
)
button = sg.Button("OK", key = '-BUTTON-')
layout = [[text_box], [button]]
window = sg.Window('Entry Test', layout, finalize=True,
resizable=True, size=(500,500), location=(800,0))
while True: # Event Loop
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == "-BUTTON-":
line = "Apple Banana Clementine"
window['-TEXT-'].update(value=line)
How can I have a text element that
- Expands to fill its available width without having an explicit width set,
- Has an explicit height in lines, and
- Updates its text normally, one line per line with all the characters in a row?