I am new new to Python/PySimpleGUI, so sorry if this would be incredibly obvious. (Also, yes, this is code I am using from a Youtube tutorial, I am not trying to take any credit for the code below.)
I have a simple program using pandas, pysimplegui, and openpyxl to input a set of values from the end user (using PySimpleGUI windows) into an Excel sheet; however, there are certain fields that I want to keep constant (such as "date" being today.strftime("%m/%d/%y")). Here's how I have tried doing it:
Defining what I want constant as variables:
bkey = 'My Name'
today = date.today()
date = today.strftime("%m/%d/%y")
And then, in pysimplegui, letting the sg.inputtext() be the variables, e.g.:
[sg.Text('Entry By', size=(15,1)), sg.InputText(bkey)]
For reference, here is a window with key as the input:
[sg.Text('To Department', size=(15,1)), sg.InputText(key='To Department')]
And then in the event of user clicking "Submit" or "Clear," calling def Clear_Input():
def clear_input():
for key in values:
window[key]('')
return None
I have no idea why clear_input() is clearing ALL values in the window, because to me it is saying any values as key in the window == '', so anything NOT defined as key (such as bkey) shouldn't be cleared.
Let me know if there is any more info I can give, I'd be glad to give it. Again, newb, sorry in advance.
Here is the full source code:
import PySimpleGUI as sg
import pandas as pd
from datetime import date
#color add to window_height
sg.theme('Topanga')
EXCEL_FILE = r'C:\Users\User\Desktop\DataEntry.xlsx'
df = pd.read_excel(EXCEL_FILE)
bkey = 'My Name'
today = date.today()
date = today.strftime("%m/%d/%y")
layout = [
[sg.Text('Please fill out the following fields:')],
[sg.Text('Service Tag', size=(15,1)), sg.InputText(key='Tag')],
[sg.Text('Serial Number (EPB)', size=(15,1)), sg.InputText(key='Serial Number')],
[sg.Text('Type of Equipment', size=(15,1)), sg.Combo(['Desktop', 'Laptop', 'Monitor', 'Tablet'], key='Type of Equipment')],
[sg.Text('New/Move', size=(15,1)), sg.InputText(key='New/Move')],
[sg.Text('User\'s Name', size=(15,1)), sg.InputText(key='User\'s Name')],
[sg.Text('From Room', size=(15,1)), sg.InputText(key='From Room')],
[sg.Text('To Room', size=(15,1)), sg.InputText(key='To Room')],
[sg.Text('To Department', size=(15,1)), sg.InputText(key='To Department')],
[sg.Text('Date', size=(15,1)), sg.InputText(date)],
[sg.Text('Attached Tag', size=(15,1)), sg.InputText(key='Attached Tag')],
[sg.Text('Entry By', size=(15,1)), sg.InputText(bkey)],
[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()
CodePudding user response:
PySimpleGui.Window.read()
returns two things: event
and values
. event
contains a description of whatever event triggered the message. values
contains a dictionary of all of the input components in your layout. The key in that dictionary is the key
parameter from your Input
item. The value
is the current value of that input item.
So, clear_input
is just running through the set of Input
elements in your layout, and setting their values to blank.
The key item is that names, like bkey
and key
have no meaning at runtime. They just hold references to objects. The Input
objects all have key values, but they are the strings you set up in your layout.
CodePudding user response:
There will be a default key generated for Input element or some other element if you don't specify the value for both of option key and option k.
Some statements updated here for you code
today = date.today()
now = today.strftime("%m/%d/%y") # date is the libray name
# In layout
[sg.Text('Date', size=(15,1)), sg.InputText(date, key='Date')], # Define the key
[sg.Text('Attached Tag', size=(15,1)), sg.InputText(key='Attached Tag')],
[sg.Text('Entry By', size=(15,1)), sg.InputText(bkey, key='Entry By')], # Define the key
# Tuple for the input elements not to clear
not_to_clear = ('Entry By', 'Date')
def clear_input():
# For elements in the window
for key, element in window.key_dict():
# If element is Input element and not in the not-to-clear tuple
if isinstance(element, sg.Input) and key not in not_to_clear:
element.update('')