Table
A Table widget displays tabular data organized into rows and columns with fixed headers. It supports custom column sizes, individual column text alignments, multi-row selection, and automatic column sorting when headers are clicked.
Examples
Workout Tracker
This app shows how rows of data in a table can be added during runtime.
import gooeypie as gp
def add_exercise(event): """Adds the workout to the table and clears the entry fields.""" exercise_tbl.add_row([exercise_entry.text, reps_entry.text]) exercise_entry.clear() reps_entry.clear() exercise_entry.focus()
app = gp.GooeyPieApp("Workout Tracker")
# Create labels, entries, and buttonexercise_lbl = gp.Label('Exercise')exercise_entry = gp.Entry()reps_lbl = gp.Label('Reps')reps_entry = gp.Entry()add_btn = gp.Button('Add', add_exercise)
# Create table and set optionsexercise_tbl = gp.Table(['Exercise', 'Reps'])exercise_tbl.set_column_widths(120, 80)exercise_tbl.height = 5
# Add widgets to the appapp.add(exercise_lbl, 1, 1, align_horizontal="left")app.add(exercise_entry, 2, 1, expand_horizontal=True)app.add(reps_lbl, 1, 2, align_horizontal="left")app.add(reps_entry, 2, 2, expand_horizontal=True)app.add(add_btn, 2, 3, expand_horizontal=True)app.add(exercise_tbl, 1, 4, expand_horizontal=True, column_span=2)
app.set_column_weights(0, 1)
app.run()Creating a Table widget
my_table = gp.Table(headings)Parameters
-
headings(list of strings): A list of strings corresponding to the headings of the table columns.
Properties
| Name | Type | Description |
|---|---|---|
.data | list of lists | Gets or sets all data in the table as a list of lists (or tuples). Each inner list must match the number of columns. |
.selected | list/tuple, or list of lists if multiple_selection is enabled | Returns the selected data row(s) in the table. Returns None if no row is selected. |
.selected_row | integer, or list of integers if multiple_selection is enabled | Gets or sets the index (or indices), starting from 0, of the selected row(s). |
.multiple_selection | boolean | Gets or sets whether multiple row selection is enabled for the table. |
.height | integer | Gets or sets the height of the table as the number of visible lines. |
.width | integer | Gets or sets the total width of the table in pixels. |
.disabled | boolean | Gets or sets whether the table widget is disabled. |
.sortable | boolean | Gets or sets whether the data can be sorted by clicking on the column headings. |
Methods
| Name | Returns | Description |
|---|---|---|
.add_row(data) | None | Adds a row of data to the end of the table. data must be a list or tuple with the same number of items as the table has columns. |
.add_row_to_top(data) | None | Adds a row of data to the top of the table. data must be a list or tuple with the same number of items as the table has columns. |
.add_row_at(index, data) | None | Adds a row of data to the table at a specified index. data must be a list or tuple with the same number of items as the table has columns. |
.remove_row(index) | list | Removes and returns the row of data at the specified index from the table. |
.remove_selected() | list, or list of lists if multiple_selection is enabled | Removes and returns the currently selected row(s) from the table. |
.clear() | None | Removes all data rows from the table. |
.select_row(index) | None | Selects the row at the specified index. |
.select_all() | None | Selects all rows in the table if multiple_selection is enabled. |
.select_none() | None | Clears any selected rows in the table. |
.set_column_width(column, width) | None | Sets the width in pixels of the specified column index (starting from 0). |
.set_column_widths(*widths) | None | Sets the widths in pixels for all columns of the table in order. |
.set_column_alignment(column, align) | None | Sets the alignment of content in the specified column index. Must be "left", "center", or "right". |
.set_column_alignments(*aligns) | None | Sets the alignment of all columns in order. Values must be "left", "center", or "right". |
Events
This widget supports the following events as well as the standard events listed below.
| Name | Description |
|---|---|
.on_change(event_function) | The function to call when a row is selected or deselected. This event is triggered whenever the user selects or deselects a row in the table, or when the selection is changed programmatically. |
Standard events
.on_click.on_double_click.on_right_click.on_middle_click.on_mouse_down.on_mouse_up.on_mouse_enter.on_mouse_leave.on_key_press.on_focus_gained.on_focus_lost
Styles
How to specify colors
- Colors can be specified as either a hex code (e.g.
"#FF0000"), a CSS color name, or"transparent". - When specifying a color for a style, you can provide either a single color or two colors separated by a comma. If you provide two colors, the first will be used for light mode and the second for dark mode. For example,
my_widget.text_color = "steelblue", "skyblue"would specify steelblue for light mode and sky blue for dark mode.
| Name | Description | Default |
|---|---|---|
.style.table_bg_color | The background color of the table area. | |
.style.border_color | The color of the border around the table. | |
.style.font_name | The font family of the text inside the table cells. | |
.style.font_size | The font size of the text inside the table cells. | |
.style.font_style | The font style of the text inside the table cells ("normal" or "italic"). | |
.style.font_weight | The font weight of the text inside the table cells ("normal" or "bold"). | |
.style.text_color | The text color of the data cells. | |
.style.selected_color | The background color of the selected row(s). | |
.style.header_bg_color | The background color of the table header row. | |
.style.header_text_color | The text color of the column headings. | "white" |
.style.header_font_name | The font family of the column headings. | |
.style.header_font_size | The font size of the column headings. | |
.style.header_font_weight | The font weight of the column headings. | |
.style.header_font_style | The font style of the column headings. |