The action to delete selected task won't be executed for always False
case.
See comment in the code here.
selected_rows = values['-TABLE-'] # For example [1,2,3,4,5]
if selected_rows:
selected_row = selected_rows[0] # 1 which is not a dict, but int
if isinstance(selected_row, dict): # It will be always False
""" Following statements won't be executed forever. """
How to work ? It depend on the data provided for the table.
values['-TABLE-']
get the list for the row numbers of selected rows
values['-TABLE-'][0]
get the 1st row number if values['-TABLE-']
is not empty.
- You can get the row data by the row number and your full data for the Table element.
- then you can get the task_id in the row data.
It will be something like this
selected_rows = values['-TABLE-'] # All selected-row numbers
if selected_rows: # If selected-row numbers is not an empty list
selected_row = selected_rows[0] # The selected row number will be indexed at 0
"""
Where the your_data is the list of list which you used when initiate the Table element.
If the task_id is at the index 0 of a list for a row task.
"""
task_id = your_data[selected_row][0] # Get the data record for the task from index which is the row number, and task_id indexed 0
selected_task = session.query(Task).get(task_id)
if selected_task:
session.delete(selected_task)
session.commit()
sg.popup('Task is deleted successfully')
del your_data[selected_row] # Remove the selected item from your data
window['-TABLE-'].update(values=your_data) # Update the Table element with updated data
else:
sg.popup('Task is not found')
else:
sg.popup('No task selected')
Another example without SQLAlchemy here
import PySimpleGUI as sg
headings = ['Task', 'Planned Action', 'Time', 'Target', 'Criterion']
your_data = [
['T1', 'Initial Interview with Client Create and agree upon the Success Criteria', '1 Hours', '19th March', 'A'],
['T2', 'Create Mock Up for Page 1 Finish Iteration 1 for UI for Page 1', '1 Hours', '20th March', 'B'],
['T3', 'Create Mock Up for Pages 2, 3 and 4 Finish Iteration 1 for UI for Pages 2, 3 and 4', '3 Hours', '23rd March', 'B'],
['T4', 'Second Meeting with Client Get feedback on UI designs', '1 Hours', '25th March', 'B'],
['T5', 'Plan the structure of the product Finish UML Diagram Iteration 1', '1 Hours', '26th March', 'B'],
['T6', 'Create Mock Up for Pages 1 to 5 Use feedback for the client for iteration 2 of the UI', '2 Hours', '27th March', 'B'],
['T7', 'Start Programming on Page 1 Create the layout for the Page and test buttons', '2 Hours', '28th March', 'C'],
['T8', 'Continue Programming Page 1 Add logic to buttons so that they can help the user move from screen to screen', '1 Hours', '29th March', 'C'],
['T9', 'Install Cloud Database for the project Test Cloud Database for Project', '1 Hours', '30th March', 'C'],
['T10', 'Create Database Structure Design Finish with initial iteration for Database Structure', '2 Hours', '31st March', 'B'],
]
col_widths = list(map(lambda y:max(y)+2, map(lambda x:map(len, x), zip(*([headings]+your_data)))))
sg.theme('LightBlue')
sg.set_options(font=("Courier New", 11))
layout = [
[sg.Table(
your_data,
headings=headings,
auto_size_columns=False,
col_widths=col_widths,
cols_justification="clccc",
select_mode=sg.TABLE_SELECT_MODE_BROWSE,
key='-TABLE-')],
[sg.Push(),
sg.Button('Delete')]
]
window = sg.Window('Demo', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Delete':
selected_rows = values['-TABLE-']
if selected_rows:
selected_row = selected_rows[0]
task_id = your_data[selected_row][0]
print(task_id)
# selected_task = session.query(Task).get(task_id)
# if selected_task:
# session.delete(selected_task)
del your_data[selected_row]
window['-TABLE-'].update(values=your_data)
# session.commit()
sg.popup('Task is deleted successfully')
# else:
# sg.popup('Task is not found')
else:
sg.popup('No task selected')
window.close()
