Reputation: 16375
I have some tabular data, that I want displayed in my GUI. I am using PySimpleGUI.
I am building on this answer.
[sg.Table(values=data_values, headings=data_headings,
max_col_width=65,
col_widths=data_cols_width,
auto_size_columns=False,
select_mode=sg.TABLE_SELECT_MODE_BROWSE,
justification='left',
enable_events=True,
num_rows=6, key='_tracker_')]
The table has enabled events, hence every click on it creates a event with the key value.
while True:
event, values = window.read()
if event == "_tracker_":
print(values["_tracker_"])
I can obtain the row of the click with values["_tracker_"]
. Can I get the column as well?
Basically I want the event to be triggered only when the table is clicked in a specific column, while the row of the event is variable. Hence, I thought the best way was to check events on whole table and than filter out based on row and column.
If there is a completely other way to do this, I am open to suggestions.
Upvotes: 1
Views: 3068
Reputation: 13061
It can work by tkinter code.
event
stored in element.user_bind_event
identify('region', x, y)
of widget of element.identify_row(y)
of widget of element.identify_column(x)
of widget of element.Here's my code for it.
import PySimpleGUI as sg
sg.theme("DarkBlue3")
newlist = [[f"Cell ({row+1}, {col+1})" for col in range(8)] for row in range(10)]
COL_HEADINGS = ["Date", "Ref", "ID", "Owner", "Customer", "Price", "Size", "Path"]
layout = [
[sg.Table(values=newlist, headings=COL_HEADINGS, max_col_width=25,
num_rows=10, alternating_row_color='green', key='-TABLE-',
enable_events=True, justification='center',)],
[sg.Text("", size=(50, 1), key='-Position-')]
]
window = sg.Window('Table', layout, finalize=True)
table = window['-TABLE-']
table.bind('<Button-1>', "Click")
position = window['-Position-']
position.expand(expand_x=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == '-TABLE-':
pass
elif event == '-TABLE-Click':
e = table.user_bind_event
region = table.Widget.identify('region', e.x, e.y)
if region == 'heading':
row = 0
elif region == 'cell':
row = int(table.Widget.identify_row(e.y))
elif region == 'separator':
continue
else:
continue
column = int(table.Widget.identify_column(e.x)[1:])
position.update(f"Table clicked at ({row}, {column})")
window.close()
Upvotes: 2