Raghu Monavarthi
Raghu Monavarthi

Reputation: 17

How do we extract the data of specific row from excel data to pysimplegui input fileds

Pysimplegui: the number that I input in texbox should be passed to a function that performs a search in an excel file. The result of that search should then appear in other remaining textbox please find the code and gui interface below in the screen shot[enter image description here][1]

[enter image description here][2]6Q84r.jpg

    import PySimpleGUI as sg
    import pandas as pd

    # Add some color to the window
    sg.theme('DarkTeal9')

    EXCEL_FILE = 'Data_Entry.xlsx'
    df = pd.read_excel(EXCEL_FILE)

    layout = [
        [sg.Text('Please fill out the following fields:')],
        [sg.Text('Name', size=(15,1)), sg.InputText(key='Name')],
        [sg.Text('City', size=(15,1)), sg.InputText(key='City')],
        [sg.Text('Favorite Colour', size=(15,1)), sg.Combo(['Green', 'Blue',                    'Red'], key='Favorite Colour')],
        [sg.Text('I speak', size=(15,1)),
                                sg.Checkbox('German', key='German'),
                                sg.Checkbox('Spanish', key='Spanish'),
                                sg.Checkbox('English', key='English')],
        [sg.Text('No. of Children', size=(15,1)), sg.Spin([i for i in range(0,16)],initial_value=0, key='Children')],
        [sg.Submit(), sg.Button('Clear'), sg.Exit()]
    ]

    window = sg.Window('Simple data entry form', layout)
    def clear_input():
        for key in values:
            window[key]('')
        return None

    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED or event == 'Exit':
            break
        if event == 'Clear':
            clear_input()
        if event == 'Submit':
            df = df.append(values, ignore_index=True)
            df.to_excel(EXCEL_FILE, index=False)
            sg.popup('Data saved!')
            clear_input()
    window.close()

Upvotes: 0

Views: 652

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

Add option enable_events=True into you sg.Input for Name, then add one more statement in your event loop to check the value of Name if match any name in column Name.

    if event == 'Name':
        name = values['Name']
        if name == '':
            continue
        index = df[df.Name==name].first_valid_index()
        if index is not None:
            lst = df.iloc[index].to_list()
            for key, value in zip(['Name', 'City', 'Favorite Colour', 'German', 'Spanish', 'English', 'Children'], lst):
                window[key].update(value)

Upvotes: 1

Related Questions