I am attempting to create a data entry form using PySimpleGUI and I want to prompt the user for a number and use that number as the range for inputting data (e.g. if the user enters 30, they can exactly enter 30 inputs).
Here is my code and I would greatly appreciate any suggestions:
import PySimpleGUI as sg
import pandas as pd
import openpyxl as xl
# Set theme
sg.theme('Topanga')
# Read in excel file
EXCEL_FILE = 'data_entry.xlsx'
df = pd.read_excel(EXCEL_FILE)
# Set layout
layout = [
[sg.Text('Please fill out the following fields : ')],
[sg.Text('Counter', size=(15,1)), sg.InputText(key='Counter',default_text = f"{df['Counter'].iloc[-1]} was your last input")],
[sg.Text('Material', size=(15,1)), sg.Combo(['Cap', 'Crystal', 'pin'], key='Material')],
[sg.Text('Color', size=(15,1), enable_events = True), sg.Combo(['C', 'G', 'Y', 'B'], key='Color')],
[sg.Text('Supplier', size=(15, 1)),sg.InputText(key='Supplier')],
[sg.Text('Shift', size=(15,1)), sg.Combo(['Day', 'Night'], key='Shift')],
[sg.Text('Time', size=(15, 1)),sg.InputText(key='Time')],
[sg.Text('Date', size=(15, 1)),sg.InputText(key='Date')],
[sg.Submit(), sg.Button('Clear'), sg.Exit()],
[sg.Button('Stop')]
]
window = sg.Window('Data Entry Form', layout)
# Define clear_input function
def clear_input():
for key in values:
window[key]('')
return None
# Begin event loop
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()
if event == 'Stop' :
path1 = 'data_entry.xlsx'
path2 = 'data_copy.xlsx'
wb1 = xl.load_workbook(filename = path1)
ws1 = wb1.worksheets[0]
wb2 = xl.load_workbook(filename = path2)
ws2 = wb2.create_sheet(ws1.title)
for row in ws1 :
for cell in row :
ws2[cell.coordinate].value = cell.value
wb2.save(path2)
sg.popup('Data Copied !')
window.close()