From 64b6ef95f5079a686520813f31ab2d9a3d502d29 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 16 Nov 2019 13:21:10 -0500 Subject: [PATCH] New demo showing how to create an Excel-like table and use arrow keys to move around --- .../Demo_Table_Simulation_Arrow_Keys.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 DemoPrograms/Demo_Table_Simulation_Arrow_Keys.py diff --git a/DemoPrograms/Demo_Table_Simulation_Arrow_Keys.py b/DemoPrograms/Demo_Table_Simulation_Arrow_Keys.py new file mode 100644 index 00000000..863874a1 --- /dev/null +++ b/DemoPrograms/Demo_Table_Simulation_Arrow_Keys.py @@ -0,0 +1,43 @@ +import PySimpleGUI as sg + +""" + Another simple table created from Input Text Elements. This demo adds the ability to "navigate" around the drawing using + the arrow keys. The tab key works automatically, but the arrow keys are done in the code below. +""" + +MAX_COLS = MAX_ROWS = 5 + +sg.change_look_and_feel('Dark Brown 1') # No excuse for gray windows + +# Create an Excel style window layout quickly and easily using list comprehensions +layout = [[sg.Text(' '*11)]+[sg.Text(s+ ' '*19) for s in 'ABCDE'] ] + \ + [[sg.T(r)] + [sg.Input('0', justification='r', key=(r,c)) for c in range(MAX_COLS)] for r in range(MAX_ROWS)] + \ + [[sg.Button('Table Values'), sg.Button('Exit')]] + +# Create the window and show it +window = sg.Window('A Table Simulation', layout, default_element_size=(12,1), element_padding=(1,1), return_keyboard_events=True) +current_cell = (0,0) +while True: # Event Loop + event, values = window.read() + if event in (None, 'Exit'): # If user closed the window + break + current_cell = window.find_element_with_focus().Key + r,c = current_cell + # Process arrow keys + if event.startswith('Down'): + r = r + 1 * (r < MAX_ROWS-1) + if event.startswith('Left'): + c = c - 1 *(c > 0) + if event.startswith('Right'): + c = c + 1 *(c < MAX_COLS-1) + if event.startswith('Up'): + r = r - 1 * (r > 0) + # if the current cell changed, set focus on new cell + if current_cell != (r,c): + current_cell = r,c + window[current_cell].set_focus() # set the focus on the element moved to + window[current_cell].update(select=True) # when setting focus, also highlight the data in the element so typing overwrites + # if clicked button to dump the table's values + if event == 'Table Values': + table = [[values[(row,col)] for col in range(MAX_COLS)] for row in range(MAX_ROWS)] + print(f'table = {table}') \ No newline at end of file