Addition of double-clicking and showing the get_liast_clicked_position function.

This commit is contained in:
PySimpleGUI 2022-07-08 08:13:13 -04:00
parent 9db1b63c01
commit 16df165603
1 changed files with 15 additions and 3 deletions

View File

@ -15,6 +15,7 @@ import operator
This demo shows how you can use these click events to sort your table by columns This demo shows how you can use these click events to sort your table by columns
Copyright 2022 PySimpleGUI
""" """
sg.theme('Light green 6') sg.theme('Light green 6')
@ -55,6 +56,7 @@ layout = [[sg.Table(values=data[1:][:], headings=headings + ['Extra'], max_col_w
auto_size_columns=True, auto_size_columns=True,
display_row_numbers=False, display_row_numbers=False,
justification='right', justification='right',
right_click_selects=True,
num_rows=20, num_rows=20,
alternating_row_color='lightyellow', alternating_row_color='lightyellow',
key='-TABLE-', key='-TABLE-',
@ -72,23 +74,33 @@ layout = [[sg.Table(values=data[1:][:], headings=headings + ['Extra'], max_col_w
# ------ Create Window ------ # ------ Create Window ------
window = sg.Window('The Table Element', layout, window = sg.Window('The Table Element', layout,
ttk_theme='clam', # ttk_theme='clam',
resizable=True) resizable=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT, finalize=True)
# Add the ability to double-click a cell
window["-TABLE-"].bind('<Double-Button-1>' , "+-double click-")
# ------ Event Loop ------ # ------ Event Loop ------
while True: while True:
event, values = window.read() event, values = window.read()
print(event, values) print(event, values)
if event == sg.WIN_CLOSED: if event == sg.WIN_CLOSED or event == 'Exit':
break break
if event == 'Edit Me':
sg.execute_editor(__file__)
elif event == 'Version':
sg.popup_scrolled(__file__, sg.get_versions(), location=window.current_location(), keep_on_top=True, non_blocking=True)
if event == 'Double': if event == 'Double':
for i in range(1, len(data)): for i in range(1, len(data)):
data.append(data[i]) data.append(data[i])
window['-TABLE-'].update(values=data[1:][:]) window['-TABLE-'].update(values=data[1:][:])
elif event == 'Change Colors': elif event == 'Change Colors':
window['-TABLE-'].update(row_colors=((8, 'white', 'red'), (9, 'green'))) window['-TABLE-'].update(row_colors=((8, 'white', 'red'), (9, 'green')))
elif event == '-TABLE-+-double click-':
print('Last cell clicked was', window['-TABLE-'].get_last_clicked_position())
if isinstance(event, tuple): if isinstance(event, tuple):
# TABLE CLICKED Event has value in format ('-TABLE=', '+CLICKED+', (row,col)) # TABLE CLICKED Event has value in format ('-TABLE=', '+CLICKED+', (row,col))
# You can also call Table.get_last_clicked_position to get the cell clicked
if event[0] == '-TABLE-': if event[0] == '-TABLE-':
if event[2][0] == -1 and event[2][1] != -1: # Header was clicked and wasn't the "row" column if event[2][0] == -1 and event[2][1] != -1: # Header was clicked and wasn't the "row" column
col_num_clicked = event[2][1] col_num_clicked = event[2][1]