PySimpleGUI - Table Element



The Table object is a useful GUI widget in any GUI library. Its purpose is to display a two-dimensional data structure of numbers and strings in a tabular form having rows and columns.

The important parameters to be passed to the Table class constructor are −

PySimpleGUI.Table(values, headings, col_widths,
   auto_size_columns, select_mode,
   display_row_numbers, num_rows,
   alternating_row_color,
   selected_row_colors,
   header_text_color)

The following table explains the role of each of these parameters −

Sr.No. Parameter & Description
1 Values

Table data represented as a 2-dimensions table

2 Headings

The headings to show on the top line

3 col_widths

Number of characters that each column will occupy

4 auto_size_columns

If True columns will be sized automatically

5 select_mode

Select Mode. Valid values:

  • TABLE_SELECT_MODE_NONE

  • TABLE_SELECT_MODE_BROWSE

  • TABLE_SELECT_MODE_EXTENDED

6 display_row_numbers

If True, the first column of the table will be the row

7 num_rows

The number of rows of the table to display at a time

8 alternating_row_color

If True every other row will have this color in the background.

9 selected_row_colors

Sets the text color and background color for a selected row.

10 header_text_color

sets the text color for the header

When any cell in the table is clicked, PySimpleGUI generates a tuple of CLICKED event having the table key, and the (row,col) of the clicked cell.

event: ('-TABLE-', '+CLICKED+', (0, 1))

Following code displays a list of students in a Table object on the PySimpleGUI window. A popup window appears when you click in any cell. The cell coordinates are displayed on the popup.

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()

It will produce the following output window −

Table Element

The Table object also has an update() method to dynamically update the table properties such as values, num_rows, and row_color.

pysimplegui_element_class.htm
Advertisements