'How determine event type in PySImpleGui Table

I have a PySimpleGui Table. There are 2 possible action (on the rows):

  • Either clicking them (which should display more information about the selected rows).
  • Double clicking on a row (which should copy the row to a different table). Is there a way to decide if a event was a click or double click?


Solution 1:[1]

Set enable_events=True for single click and select_mode=sg.TABLE_SELECT_MODE_EXTENDED for multiple items selected at once.

Bind table with '<Double-Button-1>' for double click event.

Before a double-click event, it will be a single click event.

import PySimpleGUI as sg

headings = ['President', 'Date of Birth']
data = [
    ['Ronald Reagan', 'February 6'],
    ['Abraham Lincoln', 'February 12'],
    ['George Washington', 'February 22'],
    ['Andrew Jackson', 'March 15'],
    ['Thomas Jefferson', 'April 13'],
    ['Harry Truman', 'May 8'],
    ['John F. Kennedy', 'May 29'],
    ['George H. W. Bush', 'June 12'],
    ['George W. Bush', 'July 6'],
    ['John Quincy Adams', 'July 11'],
    ['Garrett Walker', 'July 18'],
    ['Bill Clinton', 'August 19'],
    ['Jimmy Carter', 'October 1'],
    ['John Adams', 'October 30'],
    ['Theodore Roosevelt', 'October 27'],
    ['Frank Underwood', 'November 5'],
    ['Woodrow Wilson', 'December 28'],
]

layout = [[sg.Table(data, headings=headings, enable_events=True, justification='left', key='-TABLE-')]]
window = sg.Window("Title", layout, finalize=True)
window['-TABLE-'].bind("<Double-Button-1>", " Double_Click")

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == '-TABLE-':
        print(f'Row {values[event]} selected')
    elif event == '-TABLE- Double_Click':
        print(f'Double click on {values["-TABLE-"]}')

window.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason Yang