PySimpleGUI Table issue

There are two issues in PySImpleGUI table. g

  1. Click event is not working after adding the enable_click_events=True parameter.
  2. After one click of button, the table loads data. But if clicked again it will load same data again. Though I have cleared the text fields.

If any advise to make the code better, is acceptable.

adding the code below:

import dbconnect
import PySimpleGUI as PG


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

    label_TPQUAL = PG.Text("ISA QUALIFIER: ")
    ib_TPQUAL = PG.InputText(key="TPQUAL", tooltip="Enter TP ISA 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_TRANSACTION = PG.Text("TRANSACTION: ")
    ib_TRANSACTION = PG.InputText(key="TRANS", tooltip="Enter Transaction", size=20)

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

    col1 = PG.Column([[label_TPID], [label_TPNN], [label_MAP]])
    col2 = PG.Column([[ib_TPID, ], [ib_TPNN], [ib_MAP]])
    col3 = PG.Column([[label_TPQUAL], [label_TPINTID], [label_TRANSACTION]])
    col4 = PG.Column([[ib_TPQUAL], [ib_TPINTID], [ib_TRANSACTION]])

    table_toprow = ["ISA ID", "ISA QUAL", "NICKNAME", "TRANSACTION", "INTPID", "MAP"]
    table_rows = []
    view_table = PG.Table(values=table_rows,
                          headings=table_toprow, max_col_width=25,
                          auto_size_columns=True,
                          display_row_numbers=True,
                          alternating_row_color='lightgreen',
                          justification='center',
                          key='-TABLE-',
                          selected_row_colors='red on yellow',
                          enable_events=True,
                          expand_x=True,
                          expand_y=True,
                          enable_click_events=True
                          )

    window1 = PG.Window('Inbound Operations', layout=[[col1, col2, col3, col4],
                                                      [view_button, add_button, update_button, delete_button],
                                                      [view_table]], resizable=True)

    while True:
        event1, values1 = window1.read()

        if event1 == "View":
            try:
                print(event1)
                print(values1)
                if values1["TPID"] != "":
                    get_query = f"SELECT * FROM newdb.tpinfo_inbound WHERE tpisaid like '%{values1['TPID']}%'"
                elif values1["TPNN"] != "":
                    get_query = f"SELECT * FROM newdb.tpinfo_inbound WHERE tpnickname like '%{values1['TPNN']}%'"
                results = dbconnect.dbconn_func(get_query)
                for result in results:
                    table_rows.append(list(result))
                window1['-TABLE-'].update(values=table_rows)
                window1['TPID'].update(value='')
                window1['TPQUAL'].update(value='')
                window1['TPNN'].update(value='')
                window1['TPINTPID'].update(value='')
                window1['MAP'].update(value='')
                window1['TRANS'].update(value='')
                # view_button.Update(disabled=True)
            except NameError:
                PG.popup("Enter Any Value")
        elif event1 == "Add":
            if values1["TPID"] != "" and values1["TPQUAL"] != "":
                ins_query = (f"INSERT INTO "
                             f"newdb.tpinfo_inbound "
                             f"(`tpisaid`,`tpisaqual`,`tpnickname`,`transactionid`,`tpinternalid`,`map_name`) "
                             f"VALUES ('{values1['TPID']}','{values1['TPQUAL']}',"
                             f"'{values1['TPNN']}','{values1['TRANS']}',"
                             f"'{values1['TPINTPID']}','{values1['MAP']}')")
                dbconnect.dbconn_func(ins_query)
                PG.popup_quick("Value Added")
            elif values1["TPID"] == "":
                PG.popup("ISA ID Can't be BLank")
            elif values1["TPNN"] == "":
                PG.popup("Nickname Can't be Blank")
        elif '+CLICKED+' in event:
            PG.popup("You clicked row:{} Column: {}".format(event[2][0], event[2][1]))

        elif event1 == "Update":
            sel_index = values1["-TABLE-"][0]
            print(sel_index)
            print("Update")
        elif event1 == PG.WIN_CLOSED:
            break

    window1.close()


def outbound_ops():
    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_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)

    add_button = PG.Button("Add")
    view_button = PG.Button("View")
    exit_button = PG.Button("Exit")
    update_button = PG.Button("Update")

    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]])
    col4 = PG.Column([[ib_TPQUAL], [ib_TPINTID], [ib_ISASND]])

    table_toprow = ["ISA RCV ID", "ISA RCV QUAL", "NICKNAME", "GS ID", "INTPID", "MAP", "ISA SNDER ID"]
    table_rows = []
    view_table = PG.Table(values=table_rows,
                          headings=table_toprow,
                          auto_size_columns=True,
                          display_row_numbers=False,
                          justification='center',
                          key='Table',
                          selected_row_colors='red on yellow',
                          enable_events=True,
                          expand_x=True,
                          expand_y=True,
                          enable_click_events=True
                          )

    window2 = PG.Window('Outbound Operations', layout=[[col1, col2, col3, col4],
                                                      [view_button, add_button, update_button, exit_button],
                                                      [view_table]], resizable=True)

    while True:
        event2, values2 = window2.read()

        if event2 == "View":
            # event1, values1 = window1.read() # window.read(timeout=300)
            # window["clock"].update(value=time.strftime("%b %d, %Y %H:%M:%S"))
            # print(event)
            # print(values)
            try:
                if values2["TPID"] != "":
                    get_query = f"SELECT * FROM newdb.tpinfo_outbound WHERE tpisarcvid like '%{values2['TPID']}%'"
                elif values2["TPNN"] != "":
                    get_query = f"SELECT * FROM newdb.tpinfo_outbound WHERE tpnickname like '%{values2['TPNN']}%'"
                # elif values["dd_combo"] == "Outbound" and values["TPID"] != "":
                # get_query = f"SELECT * FROM newdb.tpinfo_outbound WHERE tpisarcvid='{values['TPID']}'"
                results = dbconnect.dbconn_func(get_query)
                for result in results:
                    table_rows.append(list(result))
                window2['Table'].update(values=table_rows)
            except NameError:
                PG.popup("Enter Any Value")

        elif event2 == "Add":
            if values2["TPID"] != "" and values2["TPQUAL"] != "":
                ins_query = (f"INSERT INTO "
                             f"newdb.tpinfo_outbound"
                             f"(`tpisarcvid`,`tpisarcvqual`,`tpnickname`,`tpgsid`,"
                             f"`tpinternalid`,`map_name`,`tpisasenderid`) "
                             f"VALUES('{values2['TPID']}','{values2['TPQUAL']}',"
                             f"'{values2['TPNN']}','{values2['GSRCV']}',"
                             f"'{values2['TPINTPID']}','{values2['MAP']}',{values2['ISASND']})")
                dbconnect.dbconn_func(ins_query)
                # print(ins_query)
                PG.popup_quick("Value Added")
            elif values2["TPID"] == "":
                PG.popup("ISA ID Can't be BLank")
            elif values2["TPNN"] == "":
                PG.popup("Nickname Can't be Blank")
        elif event2 == "Exit" or event2 == PG.WIN_CLOSED:
            break

    window2.close()


PG.theme("LightBlue3")

direction_label = PG.Text("Select Direction")
direction_dd = PG.Combo(["Inbound", "Outbound"], key="dd_combo")

go_button1 = PG.Button("Go")
exit_button1 = PG.Button("Exit")

window = PG.Window('Trading Partner Management', layout=[[direction_label, direction_dd],
                                                         [go_button1, exit_button1]],
                   resizable=True)

while True:
    event, values = window.read()
    if event == "Go" and values["dd_combo"] == "Inbound":
        inbound_ops()
    elif event == "Go" and values["dd_combo"] == "Outbound":
        outbound_ops()
    elif event == "Exit" or event == PG.WIN_CLOSED:
        break
window.close()