New Demo Program - Using User Settings as a Database

This commit is contained in:
PySimpleGUI 2022-06-04 11:56:00 -04:00
parent 0a5f252a8d
commit 0e0a03cdc0
2 changed files with 67 additions and 1 deletions

View File

@ -0,0 +1,66 @@
import PySimpleGUI as sg
"""
Demo - User Settings as a Database
The PySimpleGUI User Settings APIs are implemnted to look like a dictionary to the
user and utilize JSON files to store the data. As a result, one "key" is used to
store and retrieve each "setting". This capability cab be used to implement a
simple database.
In this demo the User Settings file is used to store a user ID and data associated
with that ID. Each User ID has a dictionary stored in the User Settings file. This
dictionary is built from the values dictionary of the window. There is a map varaible
called data_map that translates between the two dictionaries.
Copyright 2022 PySimpleGUI
"""
def get_id_data(user_setting, id):
return user_setting[id]
def main():
# Maps between keys used in the User Settings an the Window itself
data_map = {'-name-': '-NAME-', '-password-': '-PASSWORD-', '-dept-': '-DEPT-', '-security-': '-SECURITY-'}
user_data = sg.UserSettings('my_user_data.json')
INPUT_SIZE=30
layout = [ [sg.Text('User ID Management')],
[sg.Push(), sg.Text('User ID:'), sg.Input(key='-ID-', size=INPUT_SIZE)],
[sg.Push(), sg.Text('Name:'), sg.Input(key='-NAME-', size=INPUT_SIZE,)],
[sg.Push(), sg.Text('Password:'), sg.Input(key='-PASSWORD-', size=INPUT_SIZE, password_char='*')],
[sg.Push(), sg.Text('Department:'), sg.Input(key='-DEPT-', size=INPUT_SIZE,)],
[ sg.Text('Security Level:'), sg.Combo(('Low', 'Medium', 'High'), size=(INPUT_SIZE-2,3), readonly=True, default_value='Low', key='-SECURITY-')],
[sg.Button('Add/Update'), sg.Button('Load'), sg.Button('Display'), sg.Button('Exit')] ]
window = sg.Window('User Settings as Database', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
elif event == 'Add/Update':
# Make a dictionary of data for the ID being added/updated based on the window's values
user = values['-ID-']
data = {}
for setting_key, values_key in data_map.items():
data[setting_key] = values[values_key]
user_data[user] = data
sg.popup(f'Added or updated user: {values["-ID-"]}')
elif event == 'Load':
user = values['-ID-']
data = user_data[user]
for setting_key, values_key in data_map.items():
value = data[setting_key] if data is not None else ''
window[values_key].update(value)
elif event == 'Display':
user = values['-ID-']
data = user_data[user]
output = f'Detailed User Information for ID: {user}\n'
for setting_key, values_key in data_map.items():
value = data[setting_key] if data is not None else ''
output += f'{setting_key} = {value}\n'
sg.popup_scrolled(output, title='Detailed User Data')
if __name__ == '__main__':
main()

View File

@ -25087,4 +25087,4 @@ if __name__ == '__main__':
exit(0)
main()
exit(0)
def get_signature(): return b']\xba\xca\xce\xe5\x06\xe7\x95bd-T\xfa\xf0dD\xb1\x92:\xab\xa0\xfc\x1f\xc5\xd2\xb6\x1dr\xeaC\xbdL,\xec|Uz\xc9\x1cX\xa1\x93\xbe/%\x14\xca\x1b\xb5\x84h\x81\x0e\x98k\x91\xbf8\xf2\xdfH@\xa7\x1b\x01\xecG\xaf]{\xd1\x1b\xaa\x94\xab\xd9\xeen(\x03"\x1f[7\xc8\xe5\xe6/y~DR\x10\xb68Ke\'\x04/)\xfaL\xb3\x08\xbeEw\x98\x9e\xbe\xd5lp\x98\x06\x8c*\xbd\xa1\x98BT(\xe5N\x87!'
def get_signature(): return b'\x0c\xda\xad\xfc\xd8b(\xc8\x7f2\xf1"\x819\xaf\x13\xd7Sjv\x1e\x16\xe1(\x0c\xcb\x16\xb7@\xfc\xfd\x80l}\xa3\x9f\xbd\xb3\x8f>\xffc3%cb\x00!\xe5Z2\xa7`\xc7\x12\x98(cO\xa0<\x13\xa1R=\x9aFw\xa6\xe9\xbc\x84\xbbE\x89\x0c|\x9d\xeb]{~\x8d?\xaeOG\x86LKK\xf5\xf1\xf2\x17l!\x0fh\xed\xfdcn\xae\x7fC}\xad\xb1\x03\x90\xc6\xec)\xf7=-6\xa4\xeci\xce_\xc2\xe0\xff\xd0\x0b'