Simple excel forms and table

Viewed 33

I am working on a form to input data and want to link it to a table I have at least gotten to a point such as this but really cannot go any further I am sure this is simple but if someone could assist me that would be great

I am trying to get the input from the form to actually input correctly to the table so that the information can easily be pulled and gathered

import PySimpleGUI as sg
import pandas as pd

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

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


layout = [
    [sg.Text('Please fill out the following fields:')],
    [sg.Combo(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], size=(10,1))],
    [sg.Text('Select the Job:')],
    [sg.Combo(['Job 1', 'Job 2'], size=(6,1))],
    [sg.Text('What is the amount deposited:')],
    [sg.Text('Amount $', size=(8,1)), sg.InputText(key='Amount',size=(8,1)), ],

    [sg.Submit(), sg.Button('Clear'), sg.Exit()]
    ]

window = sg.Window('Child Support 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()

example of table

1 Answers

Try this,

import PySimpleGUI as sg
import pandas as pd

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

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

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
layout = [
    [sg.Text('Please fill out the following fields:')],
    [sg.Combo(months, size=(10, 1), key='Month')],
    [sg.Text('Select the Job:')],
    [sg.Combo(['Job 1', 'Job 2'], size=(6, 1), key='Job')],
    [sg.Text('What is the amount deposited:')],
    [sg.Text('Amount $', size=(8, 1)), sg.InputText(key='Amount',size=(8, 1))],
    [sg.Submit(), sg.Button('Clear'), sg.Exit()]
]
window = sg.Window('Child Support Form', layout)

def clear_input():
    for key in values:
        window[key].update('')

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Clear':
        clear_input()
    if event == 'Submit':
        amount = values['Amount']
        try:
            amount = float(amount)
        except ValueError:
            pass
        values['Amount'] = amount
        df = df.append(values, ignore_index=True)
        print(df)
        df.to_excel(EXCEL_FILE, index=False)
        sg.popup('Data saved!')
        clear_input()

window.close()
Related