Last active
May 24, 2023 06:28
-
-
Save Sven-Bo/02f9f949e54a210e52e5e8f9ac671dbd to your computer and use it in GitHub Desktop.
This code is a modification of the initial code from the video tutorial titled "How to Create an Excel Data Entry Form in 10 Minutes Using Python (No VBA) | Easy & Simple" by Sven Bosau. It adds a scrollbar to the main window layout using PySimpleGUI, allowing for easy navigation when filling out the form. The modified code provides an improved …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This code is a modification of the initial code from the video tutorial: | |
"How to Create an Excel Data Entry Form in 10 Minutes Using Python (No VBA) | Easy & Simple" | |
Video link: https://youtu.be/svcv8uub0D0 | |
Modifications: | |
- Added a scrollbar to the main window layout using sg.Column and sg.VerticalScroll. | |
""" | |
from pathlib import Path | |
import PySimpleGUI as sg | |
import pandas as pd | |
# Add some color to the window | |
sg.theme('DarkTeal9') | |
current_dir = Path(__file__).parent if '__file__' in locals() else Path.cwd() | |
EXCEL_FILE = current_dir / 'Data_Entry.xlsx' | |
df = pd.read_excel(EXCEL_FILE) | |
# Define the layout inside a scrollable column | |
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()] | |
] | |
scrollable_layout = [ | |
[sg.Column(layout, scrollable=True, vertical_scroll_only=True, size=(400, 300))] | |
] | |
window = sg.Window('Simple data entry form', scrollable_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': | |
new_record = pd.DataFrame(values, index=[0]) | |
df = pd.concat([df, new_record], ignore_index=True) | |
df.to_excel(EXCEL_FILE, index=False) | |
sg.popup('Data saved!') | |
clear_input() | |
window.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment