2019-11-17 23:00:35 +00:00
|
|
|
import PySimpleGUIWeb as sg
|
|
|
|
from random import randint
|
2019-11-16 18:21:10 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2019-11-17 23:00:35 +00:00
|
|
|
sg.change_look_and_feel('Light green 5') # No excuse for gray windows
|
2019-11-16 18:21:10 +00:00
|
|
|
|
2019-11-17 23:00:35 +00:00
|
|
|
MAX_COLS = MAX_ROWS = 5
|
2019-11-16 18:21:10 +00:00
|
|
|
# Create an Excel style window layout quickly and easily using list comprehensions
|
|
|
|
layout = [[sg.Text(' '*11)]+[sg.Text(s+ ' '*19) for s in 'ABCDE'] ] + \
|
2019-11-17 23:00:35 +00:00
|
|
|
[[sg.T(r)] + [sg.Input(randint(0,100), justification='r', key=(r,c)) for c in range(MAX_COLS)] for r in range(MAX_ROWS)] + \
|
2019-11-16 18:21:10 +00:00
|
|
|
[[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
|
2019-11-17 23:00:35 +00:00
|
|
|
elem = window.find_element_with_focus()
|
|
|
|
current_cell = elem.Key if elem else (0,0)
|
2019-11-16 18:21:10 +00:00
|
|
|
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}')
|