PySimpleGUI table individual columns text align

175 Views Asked by At

I am looking at this sample code (from https://www.tutorialspoint.com/pysimplegui/pysimplegui_table_element.htm). I haven't been able to find a working solution with a PySimpleGUI Table to have the first column (S.No.) align text to the left (and rest of columns text align centre). Could someone please tell me if this is possible? Thanks

import PySimpleGUI as psg
psg.set_options(font=("Arial Bold", 14))
toprow = ['S.No.', 'Name', 'Age', 'Marks']
rows = [[1, 'Rajeev', 23, 78],
        [2, 'Rajani', 21, 66],
        [3, 'Rahul', 22, 60],
        [4, 'Robin', 20, 75]]
tbl1 = psg.Table(values=rows, headings=toprow,
   auto_size_columns=True,
   display_row_numbers=False,
   justification='center', key='-TABLE-',
   selected_row_colors='red on yellow',
   enable_events=True,
   expand_x=True,
   expand_y=True,
 enable_click_events=True)
layout = [[tbl1]]
window = psg.Window("Table Demo", layout, size=(715, 200), resizable=True)
while True:
   event, values = window.read()
   print("event:", event, "values:", values)
   if event == psg.WIN_CLOSED:
      break
   if '+CLICKED+' in event:
      psg.popup("You clicked row:{} Column: {}".format(event[2][0], event[2][1]))
window.close()
1

There are 1 best solutions below

0
On BEST ANSWER

4.61.0.21

Added cols_justification for Table element - list or tuple of strings that indicates how each column should be justified

You can change the option justification to cols_justification, like

# New option for sg.Table
cols_justification="lccc",    # `l` for `left` and `c` for `center`.

Not all enhancements added into the PyPI version. Download from GitHub

There is code in the PySimpleGUI package that upgrades your previously pip installed package to the latest version checked into GitHub. You can use the commands psgmain to run the test harness or psgupgrade to invoke the GitHub upgrade code.

or, you can call sg.main(), like

d:\>python
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySimpleGUI as sg
>>> sg.main()

![image](https://user-images.githubusercontent.com/55352169/196456098-38004184-4538-453f-b163-e7e0151ec56b.png)