I have a table that I have formatted perfectly when printed out on the terminal. I'm using Python's BeautifulTable to format the table, and PySimpleGUI to create a multiline output window. I'd like the table to be printed to the GUI window for feedback to the user. I've done extensive Google searches, searches here on Stack Overflow, official product documentation, etc. I have set a max width on the table that is large enough, I've set static column widths to 25 and max table width to 200. But the table will automatically expand max width if the columns exceed that (according to documentation anyways). I have printed long lines to the multiline output window that exceed the width of the table to ensure it isn't a limitation of the PySimpleGUI output window. Above is an example of how it prints in the terminal vs. PySimpleGUI. No idea where to go next to figure out where the problem lies ...
Asked
Active
Viewed 421 times
0

spickles
- 635
- 1
- 12
- 21
-
1use monospace font – eyllanesc Sep 29 '21 at 21:04
-
Ok I'll try that, thanks! – spickles Sep 29 '21 at 21:06
-
Huzzah! I love it when solutions are quick and simple! Switched over to 'Courier' and all is well. Thanks! – spickles Sep 29 '21 at 21:16
-
Hmmm..... will have to add something to the cookbook about monospaced fonts. It's one of those "classic problems" in programming. I'll add to the "printing" section since that's what you're doing to create it. – Mike from PSG Sep 29 '21 at 23:54
1 Answers
0
To get text to be aligned vertically you'll need to use a mono-spaced font.
I tend to use Courier as it's a universally available font.
The Multiline Element is suggested now to use instead of the Output element as it has a lot more options available and it's very difficult to use.
This program will show you the effect of using one versus not using one.
import PySimpleGUI as sg
import random
import string
text_data = [[''.join([random.choice(string.ascii_letters) for _ in range(10)]) for _ in range(4)] for rows in range(10)]
layout = [ [sg.Text('Outputting Text that is tabular in nature')],
[sg.Multiline(size=(60,20), reroute_stdout=True, font='default 10', write_only=True, key='-MLINE-')],
[sg.Button('Print Table'),sg.Button('Courier'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout)
while True:
event, values = window.read()
# print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Print Table':
for row in text_data:
print(' '.join(row))
elif event == 'Courier':
window['-MLINE-'].update(font='courier 10')
window.close()

Mike from PSG
- 5,312
- 21
- 39