-1

I am creating a GUI for my co worker to help them with their reporting processes for our genetic testing. I'm having an issue I cant seem to figure out though. The workflow is set in the order of tabs in the program. If I use the webview feature on the AlleleTyper tab, when I move to our diplotype calculator and try to open a csv, the program shuts down. I thought I had the program running through a loop, but maybe I'm wrong. Does anyone else see something wrong?

def csv_window(file_path):
    data, header_list = read_csv_file(file_path)

    sg.popup_quick_message('Building your main window.... one moment....', background_color='#1c1e23', text_color='white', keep_on_top=True, font='_ 30')

    # ------ Window Layout ------
    layout = [  [sg.Text('Click a heading to sort on that column or enter a filter and click a heading to search for matches in that column')],
                [sg.Text(f'{len(data)} Records in table', font='_ 18')],
                [sg.Text(k='-RECORDS SHOWN-', font='_ 18')],
                [sg.Text(k='-SELECTED-')],
                [sg.T('Filter:'), sg.Input(k='-FILTER-', focus=True, tooltip='Not case sensative\nEnter value and click on a col header'),
                 sg.B('Reset Table', tooltip='Resets entire table to your original data'),
                 sg.Checkbox('Sort Descending', k='-DESCENDING-'), sg.Checkbox('Filter Out (exclude)', k='-FILTER OUT-', tooltip='Check to remove matching entries when filtering a column'), sg.Push()],
                [sg.Table(values=data, headings=header_list, max_col_width=25,
                        auto_size_columns=True, display_row_numbers=True, vertical_scroll_only=True,
                        justification='right', num_rows=50,
                        key='-TABLE-', selected_row_colors='red on yellow', enable_events=True,
                        expand_x=True, expand_y=True,
                        enable_click_events=True)],
                [sg.Sizegrip()]]

    # ------ Create Window ------
    window = sg.Window('CSV Table Display', layout, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT,  resizable=True, size=(800, 600), finalize=True)
    window.bind("<Control_L><End>", '-CONTROL END-')
    window.bind("<End>", '-CONTROL END-')
    window.bind("<Control_L><Home>", '-CONTROL HOME-')
    window.bind("<Home>", '-CONTROL HOME-')
    original_data = data        # save a copy of the data
    # ------ Event Loop ------
    while True:
        event, values = window.read()
        # print(event, values)
        if event in (sg.WIN_CLOSED, 'Exit'):
            break
        if values['-TABLE-']:           # Show how many rows are slected
            window['-SELECTED-'].update(f'{len(values["-TABLE-"])} rows selected')
        else:
            window['-SELECTED-'].update('')
        if event[0] == '-TABLE-':
        # if isinstance(event, tuple):
            filter_value = values['-FILTER-']
            # TABLE CLICKED Event has value in format ('-TABLE=', '+CLICKED+', (row,col))
            if event[0] == '-TABLE-':
                if event[2][0] == -1 and event[2][1] != -1:  # Header was clicked and wasn't the "row" column
                    col_num_clicked = event[2][1]
                    # if there's a filter, first filter based on the column clicked
                    if filter_value not in (None, ''):
                        filter_out = values['-FILTER OUT-']     # get bool filter out setting
                        new_data = []
                        for line in data:
                            if not filter_out and (filter_value.lower() in line[col_num_clicked].lower()):
                                new_data.append(line)
                            elif filter_out and (filter_value.lower() not in line[col_num_clicked].lower()):
                                new_data.append(line)
                        data = new_data
                    new_table = sort_table(data, (col_num_clicked, 0), values['-DESCENDING-'])
                    window['-TABLE-'].update(new_table)
                    data = new_table
                    window['-RECORDS SHOWN-'].update(f'{len(new_table)} Records shown')
                    window['-FILTER-'].update('')           # once used, clear the filter
                    window['-FILTER OUT-'].update(False)  # Also clear the filter out flag
        elif event == 'Reset Table':
            data = original_data
            window['-TABLE-'].update(data)
            window['-RECORDS SHOWN-'].update(f'{len(data)} Records shown')
        elif event == '-CONTROL END-':
            window['-TABLE-'].set_vscroll_position(100)
        elif event == '-CONTROL HOME-':
            window['-TABLE-'].set_vscroll_position(0)
        elif event == 'Edit Me':
            sg.execute_editor(__file__)
        elif event == 'Version':
            sg.popup_scrolled(__file__, sg.get_versions(), location=window.current_location(), keep_on_top=True, non_blocking=True)
    window.close()


ClarkThark
  • 25
  • 7
  • Please read [how to ask](https://stackoverflow.com/help/how-to-ask) and edit your question providing a [_Minimal_, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Jorge Luis Apr 27 '23 at 15:27
  • I wasn't sure where the issue was, hence the block of code. I will change it to just the event loop in hopes that therein lies the problem area. – ClarkThark Apr 27 '23 at 15:33
  • Maybe something wrong at the code you didn't provide. – Jason Yang Apr 27 '23 at 16:21
  • I was downvoted for having the entire code here, and I'm not quite sure which section it is. Should I just keep editing and posting different sections? – ClarkThark Apr 27 '23 at 16:30
  • 2
    You should find the minimal code that causes the error and can be reproduced by the people that tries to help you. You have to narrow it down to something more specific. It's not just chopping it to make it small. – Poshi Apr 27 '23 at 17:24
  • cant find import PySimpleGUI as sg in your code – pippo1980 Apr 27 '23 at 21:32

1 Answers1

0

Sorry everyone, and thank you so much for your help. I was able to find the issue. I had this section of code within the 'def csv_window(file_path)' function. Once I moved it to the end of the code, Everything began running normally.

Here was the problem:

if __name__ == '__main__':
    event, values = window.read()
ClarkThark
  • 25
  • 7