0

I am trying to reference a Pandas dataframe in Pysimplegui

`

# function definitions
def retrieve_info():
    if values['-UPC-'] == '':
        search_box = values['-UPC-']
        records = item_info.iloc[item_info[0] == search_box]
        for records in records:
            window['-SKU-'].update(records[item_info['Item Number']])
            window['-DESCRIPTION-'].update(records[item_info['Item Description']])
            window['-UOM-'].update(records[item_info['UOM']])
            window['-LOCATION-'].update(records[item_info['LOCATION']])
          
        

`

trying to get the fields below Scan Product to populate with information but instead get this error. KeyError: 'UPC' GUI

3
  • Please provide enough code so others can better understand or reproduce the problem. Commented Oct 25, 2022 at 16:47
  • search_box will be an empty string. Search where the string 'UPC' in your script and find what's the correct key. Commented Oct 25, 2022 at 16:49
  • Yes its currently empty however, the idea is when the user scans a UPC from a product into the field Scan Product: then the other inputs will return a value from the excel that is uploaded into the GUI. UPC in the excel document is the index. Commented Oct 25, 2022 at 17:35

1 Answer 1

0

Demo code for you

import pandas as pd
import PySimpleGUI as sg


# table = pd.read_excel('table.xlsx')
data = [[(i+1)*(j+1) for i in range(9)] for j in range(9)]
table = pd.DataFrame(data, columns=[i+1 for i in range(9)])

layout = [
    [sg.Text("A (1~9)"), sg.Input(size=5, enable_events=True, key='A')],
    [sg.Text("B (1~9)"), sg.Input(size=5, enable_events=True, key='B')],
    [sg.Text(f"A x B = ", expand_x=True, key='Result')],
]
window = sg.Window('Title', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event in 'AB':
        a, b = values['A'], values['B']
        if a and a in "123456789" and b and b in "123456789":
            window['Result'].update(f'{a} x {b} = {table[int(a)][int(b)-1]}')
        else:
            window['Result'].update('Wrong A or B')


window.close()

enter image description here

There's nothing special for the table.xlsx, it is something like this. enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

thanks jason do you happen to have the excel table you used so i can see how it works.
There's nothing special for the xlsx file, top row for the columns, updated as above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.