Display data in a table format
()
| 10 | |
| 11 | |
| 12 | def TableSimulation(): |
| 13 | """ |
| 14 | Display data in a table format |
| 15 | """ |
| 16 | |
| 17 | sg.popup_quick_message('Hang on for a moment, this will take a bit to create....', auto_close=True, non_blocking=True, font='Default 18') |
| 18 | |
| 19 | sg.set_options(element_padding=(0, 0)) |
| 20 | |
| 21 | menu_def = [['File', ['Open', 'Save', 'Exit']], |
| 22 | ['Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ], |
| 23 | ['Help', 'About...'], ] |
| 24 | |
| 25 | MAX_ROWS = 20 |
| 26 | MAX_COL = 10 |
| 27 | |
| 28 | columm_layout = [[sg.Text(str(i), size=(4, 1), justification='right')] + [sg.InputText(size=(10, 1), pad=(1,1),border_width=0, justification='right', key=(i, j)) for j in range(MAX_COL)] for i in range(MAX_ROWS)] |
| 29 | |
| 30 | |
| 31 | layout = [[sg.Menu(menu_def)], |
| 32 | [sg.Text('Table Using Combos and Input Elements', font='Any 18')], |
| 33 | [sg.Text('Type in a row, column and value. The form will update the values in realtime as you type'), |
| 34 | sg.Input(key='inputrow', justification='right', size=(8, 1), pad=(1, 1)), |
| 35 | sg.Input(key='inputcol', size=(8, 1), pad=(1, 1), justification='right'), |
| 36 | sg.Input(key='value', size=(8, 1), pad=(1, 1), justification='right')], |
| 37 | [sg.Col(columm_layout, size=(800, 600), scrollable=True)]] |
| 38 | |
| 39 | window = sg.Window('Table', layout, return_keyboard_events=True, resizable=True) |
| 40 | |
| 41 | while True: |
| 42 | event, values = window.read() |
| 43 | # --- Process buttons --- # |
| 44 | if event in (sg.WIN_CLOSED, 'Exit'): |
| 45 | break |
| 46 | elif event == 'About...': |
| 47 | sg.popup('Demo of table capabilities') |
| 48 | elif event == 'Open': |
| 49 | filename = sg.popup_get_file( |
| 50 | 'filename to open', no_window=True, file_types=(("CSV Files", "*.csv"),)) |
| 51 | # --- populate table with file contents --- # |
| 52 | if filename is not None: |
| 53 | with open(filename, "r") as infile: |
| 54 | reader = csv.reader(infile) |
| 55 | try: |
| 56 | # read everything else into a list of rows |
| 57 | data = list(reader) |
| 58 | except: |
| 59 | sg.popup_error('Error reading file') |
| 60 | continue |
| 61 | # clear the table |
| 62 | [window[(i, j)].update('') for j in range(MAX_COL) |
| 63 | for i in range(MAX_ROWS)] |
| 64 | |
| 65 | for i, row in enumerate(data): |
| 66 | for j, item in enumerate(row): |
| 67 | location = (i, j) |
| 68 | try: # try the best we can at reading and filling the table |
| 69 | target_element = window[location] |
no test coverage detected