Python Forum
[PyGUI] Two issues in table created by PySimpleGUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] Two issues in table created by PySimpleGUI
#11
(Oct-11-2023, 03:33 PM)ranjansanyal2007 Wrote:
(Oct-03-2023, 05:48 PM)ranjansanyal2007 Wrote: rt
As the result coming as tuple, I had to convert it to a list and it worked with comprehension.
Also the table I am taking the -TABLE- from the event and it is working as expected.

Quote:I have made some success, but still struggling to create a button, which can refresh the table and clear. I have created the "Clear" button. Sharing the code below.
import dbconnect
import PySimpleGUI as PG


def outbound_ops():
    PG.theme("LightBlue3")

    label_TPID = PG.Text("ISA RCV ID: ")
    ib_TPID = PG.InputText(key="TPID", tooltip="Enter TP ISA ID", size=30)

    label_TPQUAL = PG.Text("ISA RCV QUAL: ")
    ib_TPQUAL = PG.InputText(key="TPQUAL", tooltip="Enter TP ISA Qualifier", size=20)

    label_sndq = PG.Text("ISA SND QUAL: ")
    ib_sndq = PG.InputText(key="SNDQUAL", tooltip="Enter Sender Qualifier", size=20)

    label_TPNN = PG.Text("TP NICKNAME: ")
    ib_TPNN = PG.InputText(key="TPNN", tooltip="Enter TP NickName", size=30)

    label_TPINTID = PG.Text("TP INTPID: ")
    ib_TPINTID = PG.InputText(key="TPINTPID", tooltip="Enter INTPID", size=30)

    label_MAP = PG.Text("MAP NAME: ")
    ib_MAP = PG.InputText(key="MAP", tooltip="Enter MAP NAME", size=30)

    label_ISASND = PG.Text("ISA SENDER ID: ")
    ib_ISASND = PG.InputText(key="ISASND", tooltip="Enter ISA Sender", size=30)

    label_GSRCV = PG.Text("GS RCV ID: ")
    ib_GSRCV = PG.InputText(key="GSRCV", tooltip="Enter GS Receiver", size=30)

    label_gssnd = PG.Text("GS SND ID: ")
    ib_gssnd = PG.InputText(key="GSSND", tooltip="Enter GS Sender", size=30)

    label_gsver = PG.Text("GS VERS: ")
    ib_gsver = PG.InputText(key="GSVER", tooltip="Enter GS Version", size=30)

    label_isaver = PG.Text("ISA VERS: ")
    ib_isaver = PG.InputText(key="ISAVER", tooltip="Enter ISA Version", size=30)

    label_desc = PG.Text("DESCRIPTION: ")
    ib_desc = PG.InputText(key="DESC", tooltip="Enter Description: NickName_X12", size=30)

    add_button = PG.Button("Add")
    view_button = PG.Button("View")
    delete_button = PG.Button("Delete")
    update_button = PG.Button("Update")
    clear_button = PG.Button("Clear")

    col1 = PG.Column([[label_TPID], [label_TPNN], [label_MAP], [label_GSRCV]])
    col2 = PG.Column([[ib_TPID, ], [ib_TPNN], [ib_MAP], [ib_GSRCV]])
    col3 = PG.Column([[label_TPQUAL], [label_TPINTID], [label_ISASND], [label_sndq]])
    col4 = PG.Column([[ib_TPQUAL], [ib_TPINTID], [ib_ISASND], [ib_sndq]])
    col5 = PG.Column([[label_isaver], [label_gssnd], [label_gsver], [label_desc]])
    col6 = PG.Column([[ib_isaver], [ib_gssnd], [ib_gsver], [ib_desc]])

    table_toprow = ["NICKNAME", "INTPID", "ISA RCV ID", "ISA RCV QUAL", "ISA SND QUAL",
                    "GS VERS", "DESCRIPTION", "GS VER", "GS RCV ID", "ISA SND ID",
                    "GS SND ID", "MAP"]
    table_rows = []
    view_table = PG.Table(values=table_rows,
                          headings=table_toprow, max_col_width=25,
                          auto_size_columns=True,
                          display_row_numbers=False,
                          alternating_row_color='lightblue',
                          justification='center',
                          key='-TABLE-',
                          selected_row_colors='white on blue',
                          enable_events=True,
                          expand_x=True,
                          expand_y=True,
                          enable_click_events=True
                          )

    window = PG.Window('Outbound Operations', layout=[[col1, col2, col3, col4, col5, col6],
                                                      [view_button, add_button, update_button,
                                                       delete_button, clear_button],
                                                      [view_table]], resizable=True)

    while True:
        event, values = window.read()
        print(event)
        print(values)

        if event in (PG.WIN_CLOSED, "Exit"):
            break
        elif event == "View":
            try:
                if values["TPNN"] != "":
                    get_query = (f"SELECT * FROM CODELIST_XREF_ITEM A, CODELIST_XREF_VERS B "
                                 f"WHERE A.LIST_NAME=B.LIST_NAME "
                                 f"and A.LIST_VERSION=B.DEFAULT_VERSION "
                                 f"and A.LIST_NAME= 'TST_CDLST_OB_FF_EDI_LKUP' "
                                 f"and SENDER_ITEM like '%{values['TPNN']}%'")
                    results = dbconnect.dbconn_func(get_query)
                    table_rows = [list(result[4:16]) for result in results]
                elif values["TPID"] != "":
                    get_query = (f"SELECT * FROM CODELIST_XREF_ITEM A, CODELIST_XREF_VERS B "
                                 f"WHERE A.LIST_NAME=B.LIST_NAME "
                                 f"and A.LIST_VERSION=B.DEFAULT_VERSION "
                                 f"and A.LIST_NAME= TST_CDLST_OB_FF_EDI_LKUP' "
                                 f"and TEXT1 like '%{values['TPID']}%'")
                    results = dbconnect.dbconn_func(get_query)
                    table_rows = [list(result[4:16]) for result in results]
                window['-TABLE-'].update(values=table_rows)
                window['TPID'].update(value='')
                window['TPQUAL'].update(value='')
                window['TPNN'].update(value='')
                window['TPINTPID'].update(value='')
                window['MAP'].update(value='')
                window['ISASND'].update(value='')
                window['GSRCV'].update(value='')
                window['SNDQUAL'].update(value='')
                window['GSSND'].update(value='')
                window['GSVER'].update(value='')
                window['ISAVER'].update(value='')
                window['DESC'].update(value='')
                add_button.update(disabled=True)
                update_button.update(disabled=True)
                delete_button.update(disabled=True)
            except NameError:
                PG.popup("Enter Any Value")
        elif event == "Add":
            if values["TPID"] != "" and values["TPQUAL"] != "":
                ins_query = (f"INSERT INTO "
                             f"newdb.tpinfo_outbound"
                             f"(`tpisarcvid`,`tpisarcvqual`,`tpnickname`,`tpgsid`,"
                             f"`tpinternalid`,`map_name`,`tpisasenderid`) "
                             f"VALUES('{values['TPID']}','{values['TPQUAL']}',"
                             f"'{values['TPNN']}','{values['GSRCV']}',"
                             f"'{values['TPINTPID']}','{values['MAP']}',{values['ISASND']})")
                dbconnect.dbconn_func(ins_query)
                PG.popup_quick("Value Added")
                window['-TABLE-'].update(values=table_rows, select_rows=None,)
                window['TPID'].update(value='')
                window['TPQUAL'].update(value='')
                window['TPNN'].update(value='')
                window['TPINTPID'].update(value='')
                window['MAP'].update(value='')
                window['ISASND'].update(value='')
                window['GSRCV'].update(value='')
                window['SNDQUAL'].update(value='')
                window['GSSND'].update(value='')
                window['GSVER'].update(value='')
                window['ISAVER'].update(value='')
                window['DESC'].update(value='')
            elif values["TPID"] == "":
                PG.popup("ISA ID Can't be BLank")
            elif values["TPNN"] == "":
                PG.popup("Nickname Can't be Blank")
        elif event == "Update":
            if values["TPID"] != "" and values["TPQUAL"] != "":
                sel_index = values["-TABLE-"][0]
                print(sel_index)
                print("Update")
            elif values["TPID"] == "":
                PG.popup("ISA ID Can't be BLank")
            elif values["TPNN"] == "":
                PG.popup("Nickname Can't be Blank")
        elif event == '-TABLE-':
            tb_index = values['-TABLE-'][0]
            window['TPID'].update(value=table_rows[tb_index][2])
            window['TPQUAL'].update(value=table_rows[tb_index][3])
            window['TPNN'].update(value=table_rows[tb_index][0])
            window['TPINTPID'].update(value=table_rows[tb_index][1])
            window['MAP'].update(value=table_rows[tb_index][11])
            window['ISASND'].update(value=table_rows[tb_index][9])
            window['GSRCV'].update(value=table_rows[tb_index][8])
            window['SNDQUAL'].update(value=table_rows[tb_index][4])
            window['GSSND'].update(value=table_rows[tb_index][10])
            window['GSVER'].update(value=table_rows[tb_index][5])
            window['ISAVER'].update(value=table_rows[tb_index][7])
            window['DESC'].update(value=table_rows[tb_index][6])
            update_button.update(disabled=False)
            delete_button.update(disabled=False)
            window['TPNN'].update(disabled=True)
        elif event == "Clear":
            window['-TABLE-'].update(values='', select_rows=None)
            window['TPID'].update(value='')
            window['TPQUAL'].update(value='')
            window['TPNN'].update(value='', disabled=False)
            window['TPINTPID'].update(value='')
            window['MAP'].update(value='')
            window['ISASND'].update(value='')
            window['GSRCV'].update(value='')
            window['SNDQUAL'].update(value='')
            window['GSSND'].update(value='')
            window['GSVER'].update(value='')
            window['ISAVER'].update(value='')
            window['DESC'].update(value='')
            update_button.update(disabled=True)
            delete_button.update(disabled=True)
            add_button.update(disabled=False)

    window.close()

Updating the latest quote. I am unable to manage any of the operations, as most of the time the tb_index is going out of range. I am not sure why.
import dbconnect
import PySimpleGUI as PG


def outbound_ops():
    PG.theme("LightBlue3")

    label_tpid = PG.Text("ISA RCV ID: ")
    ib_tpid = PG.InputText(key="TPID", tooltip="Enter TP ISA ID", size=30)

    label_tpqual = PG.Text("ISA RCV QUAL: ")
    ib_tpqual = PG.InputText(key="TPQUAL", tooltip="Enter TP ISA Qualifier", size=20)

    label_sndq = PG.Text("ISA SND QUAL: ")
    ib_sndq = PG.InputText(key="SNDQUAL", tooltip="Enter Sender Qualifier", size=20)

    label_tpnn = PG.Text("TP NICKNAME: ")
    ib_tpnn = PG.InputText(key="TPNN", tooltip="Enter TP NickName", size=30)

    label_tpintid = PG.Text("TP INTPID: ")
    ib_tpintid = PG.InputText(key="TPINTPID", tooltip="Enter INTPID", size=30)

    label_map = PG.Text("MAP NAME: ")
    ib_map = PG.InputText(key="MAP", tooltip="Enter MAP NAME", size=30)

    label_isasnd = PG.Text("ISA SENDER ID: ")
    ib_isasnd = PG.InputText(key="ISASND", tooltip="Enter ISA Sender", size=30)

    label_gsrcv = PG.Text("GS RCV ID: ")
    ib_gsrcv = PG.InputText(key="GSRCV", tooltip="Enter GS Receiver", size=30)

    label_gssnd = PG.Text("GS SND ID: ")
    ib_gssnd = PG.InputText(key="GSSND", tooltip="Enter GS Sender", size=30)

    label_gsver = PG.Text("GS VERS: ")
    ib_gsver = PG.InputText(key="GSVER", tooltip="Enter GS Version", size=30)

    label_isaver = PG.Text("ISA VERS: ")
    ib_isaver = PG.InputText(key="ISAVER", tooltip="Enter ISA Version", size=30)

    label_desc = PG.Text("DESCRIPTION: ")
    ib_desc = PG.InputText(key="DESC", tooltip="Enter Description: NickName_X12", size=30)

    add_button = PG.Button("Add")
    view_button = PG.Button("View")
    delete_button = PG.Button("Delete")
    update_button = PG.Button("Update")
    clear_button = PG.Button("Clear")

    col1 = PG.Column([[label_tpid], [label_tpnn], [label_map], [label_gsrcv]])
    col2 = PG.Column([[ib_tpid, ], [ib_tpnn], [ib_map], [ib_gsrcv]])
    col3 = PG.Column([[label_tpqual], [label_tpintid], [label_isasnd], [label_sndq]])
    col4 = PG.Column([[ib_tpqual], [ib_tpintid], [ib_isasnd], [ib_sndq]])
    col5 = PG.Column([[label_isaver], [label_gssnd], [label_gsver], [label_desc]])
    col6 = PG.Column([[ib_isaver], [ib_gssnd], [ib_gsver], [ib_desc]])

    table_toprow = ["NICKNAME", "INTPID", "ISA RCV ID", "ISA RCV QUAL", "ISA SND QUAL",
                    "GS VERS", "DESCRIPTION", "GS VER", "GS RCV ID", "ISA SND ID",
                    "GS SND ID", "MAP"]
    table_rows = []
    view_table = PG.Table(values=table_rows,
                          headings=table_toprow, max_col_width=20,
                          auto_size_columns=True,
                          display_row_numbers=False,
                          alternating_row_color='lightblue',
                          justification='left',
                          key='-TABLE-',
                          selected_row_colors='white on blue',
                          enable_events=True,
                          expand_x=True,
                          expand_y=True,
                          enable_click_events=True
                          )

    window = PG.Window('Outbound Operations', layout=[[col1, col2, col3, col4, col5, col6],
                                                      [view_button, add_button, update_button,
                                                       delete_button, clear_button],
                                                      [view_table]], resizable=True)

    def clear_fields():
        keys = ['TPID', 'TPQUAL', 'TPINTPID', 'TPNN', 'MAP', 'ISASND', 'GSRCV', 'SNDQUAL', 'GSSND',
                'GSVER', 'ISAVER', 'DESC']
        for key in keys:
            window[key].update(value='')

    while True:
        event, values = window.read()
        # add_button.update(disabled=False)
        # update_button.update(disabled=False)
        # delete_button.update(disabled=False)

        if event in (PG.WIN_CLOSED, "Exit"):
            break
        elif event == "View":
            try:
                if values["TPNN"] != "":
                    get_query = (f"SELECT * FROM CODELIST_XREF_ITEM A, CODELIST_XREF_VERS B "
                                 f"WHERE A.LIST_NAME=B.LIST_NAME "
                                 f"and A.LIST_VERSION=B.DEFAULT_VERSION "
                                 f"and A.LIST_NAME= 'TST_CDLST_OB_FF_EDI_LKUP' "
                                 f"and SENDER_ITEM like '%{values['TPNN']}%'")
                    results = dbconnect.dbconn_func(get_query)
                    table_rows = [list(result[4:16]) for result in results]
                elif values["TPID"] != "":
                    get_query = (f"SELECT * FROM CODELIST_XREF_ITEM A, CODELIST_XREF_VERS B "
                                 f"WHERE A.LIST_NAME=B.LIST_NAME "
                                 f"and A.LIST_VERSION=B.DEFAULT_VERSION "
                                 f"and A.LIST_NAME= 'TST_CDLST_OB_FF_EDI_LKUP' "
                                 f"and TEXT1 like '%{values['TPID']}%'")
                    results = dbconnect.dbconn_func(get_query)
                    table_rows = [list(result[4:16]) for result in results]
                window['-TABLE-'].update(values=table_rows)
                clear_fields()
                add_button.update(disabled=True)
                update_button.update(disabled=True)
                delete_button.update(disabled=True)
            except NameError:
                PG.popup("Enter Any Value")
        elif event == "Add":
            if values["TPID"] != "" and values["TPQUAL"] != "":
                ins_query = (f"INSERT INTO "
                             f"CODELIST_XREF_ITEM" 
                             f"(LIST_NAME,LIST_VERSION,SENDER_ITEM,RECEIVER_ITEM,TEXT1,TEXT2,"
                             f"TEXT3,TEXT4,DESCRIPTION,TEXT5,TEXT6,TEXT7,TEXT8,TEXT9) "
                             f"VALUES ('TST_CDLST_OB_FF_EDI_LKUP', "
                             f"(select DEFAULT_VERSION FROM CODELIST_XREF_VERS "
                             f"where LIST_NAME='TST_CDLST_OB_FF_EDI_LKUP'), "
                             f"'{values['TPNN']}', '{values['TPINTPID']}', "
                             f"'{values['TPID']}', '{values['TPQUAL']}', '{values['SNDQUAL']}', "
                             f"'{values['GSVER']}','{values['DESC']}', '{values['ISAVER']}', "
                             f"'{values['GSRCV']}', '{values['ISASND']}', '{values['GSSND']}', "
                             f"'{values['MAP']}')")
                dbconnect.dbconn_func(ins_query)
                PG.popup_quick("Value Added")
                window['-TABLE-'].update(values=table_rows)
                clear_fields()
            elif values["TPID"] == "":
                PG.popup("ISA ID Can't be BLank")
            elif values["TPNN"] == "":
                PG.popup("Nickname Can't be Blank")
        elif event == "Update":
            continue
        elif event == '-TABLE-':
            tb_index = values['-TABLE-'][0]
            print(tb_index)
            window['TPID'].update(value=table_rows[tb_index][2])
            window['TPQUAL'].update(value=table_rows[tb_index][3])
            window['TPNN'].update(value=table_rows[tb_index][0])
            window['TPINTPID'].update(value=table_rows[tb_index][1])
            window['MAP'].update(value=table_rows[tb_index][11])
            window['ISASND'].update(value=table_rows[tb_index][9])
            window['GSRCV'].update(value=table_rows[tb_index][8])
            window['SNDQUAL'].update(value=table_rows[tb_index][4])
            window['GSSND'].update(value=table_rows[tb_index][10])
            window['GSVER'].update(value=table_rows[tb_index][5])
            window['ISAVER'].update(value=table_rows[tb_index][7])
            window['DESC'].update(value=table_rows[tb_index][6])
            update_button.update(disabled=False)
            delete_button.update(disabled=False)
            window['TPNN'].update(disabled=True)
        elif event == 'Delete':
            if values["TPID"] != "" and values["TPQUAL"] != "":
                confirmation = PG.popup_yes_no("Do You want to delete the selected item?")
                if confirmation == "Yes":
                    delete_query = (f"DELETE FROM CODELIST_XREF_ITEM "
                                    f"WHERE LIST_NAME='TST_CDLST_OB_FF_EDI_LKUP' AND "
                                    f"LIST_VERSION=(select DEFAULT_VERSION FROM CODELIST_XREF_VERS "
                                    f"where LIST_NAME='TST_CDLST_OB_FF_EDI_LKUP') AND "
                                    f"SENDER_ITEM='{values['TPNN']}' AND "
                                    f"RECEIVER_ITEM='{values['TPINTPID']}'")
                    dbconnect.dbconn_func(delete_query)
                    PG.popup("Record Deleted")
                    clear_fields()
                    delete_button.update(disabled=True)
                    # del table_rows[values['-TABLE-'][0]]
                    # window['-TABLE-'].update(values=table_rows)
        elif event == "Clear":
            # table_rows = []
            # window['-TABLE-'].update(values=table_rows)
            clear_fields()
            window['TPNN'].update(disabled=False)
            update_button.update(disabled=True)
            delete_button.update(disabled=True)
            add_button.update(disabled=False)
            view_button.update(disabled=False)
        print(event)
        print(values)
        print(table_rows)

    window.close()


if __name__ == '__main__':
    outbound_ops()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020