PySimpleGUI - Settings



Global Settings

Global settings are the application settings available application wide. These settings control the various properties of the Element class to be applied to all the Elements in the application.

These settings work in hierarchical manner. The global settings are overridden if those settings are given different value for a window. In turn the settings defined in Window object are given different value for a specific element.

For example, if the font size is set to 16 globally, the text of all elements is displayed accordingly. However, if a specific Text or Input element with Font property with size other than 16 is defined in the layout, it will change the appearance accordingly.

The function set_options is used to change settings that will apply globally. If it's a setting that applies to Windows, then that setting will apply not only to Windows that you create, but also to popup Windows.

import PySimpleGUI as sg
sg.set_options(font=('Arial Bold', 16))

User Settings

"User settings" is a dictionary that is automatically written to your hard drive. User settings are stored in a Python dictionary which is saved to and loaded from the disk. Individual settings are thus keys into a dictionary.

List of user setting functions −

Sr.No. Function & Description
1 user_settings

Returns settings as a dictionary

2 user_settings_delete_entry

Deletes a setting

3 user_settings_delete_filename

Deletes the settings file

4 user_settings_file_exists

Returns True if settings file specified exists

5 user_settings_filename

Returns full path and filename of settings file

6 user_settings_get_entry

Returns value for a setting. If no setting found, then specified default value is returned

7 user_settings_load

Loads dictionary from the settings file.

8 user_settings_save

Saves settings to current or newly specified file.

9 user_settings_set_entry

Sets an entry to a particular value

10 user_settings_write_new_dictionary

Writes a specified dictionary to settings file

Create the User Settings object.

settings = sg.UserSettings()

Use the dictionary-style [ ] syntax to read a setting. If the item's name is '- item-', then reading the value is achieved by writing

item_value = settings['-item-']

Following sttement is used to Write the setting.

settings['-item-'] = new_value

To delete an item, again the dictionary style syntax is used.

del settings['-item-']

You can also call the delete_entry method to delete the entry.

settings.delete_entry('-item-')

The following simple program demonstrates load/saving of user settings

import PySimpleGUI as sg
import json
sg.set_options(font=('Arial Bold', 16))
layout = [
   [sg.Text('Settings', justification='left')],
   [sg.Text('User name', size=(10, 1), expand_x=True),
   sg.Input(key='-USER-')],
   [sg.Text('email ID', size=(10, 1), expand_x=True),
   sg.Input(key='-ID-')],
   [sg.Text('Role', size=(10, 1), expand_x=True),
   sg.Input(key='-ROLE-')],
   [sg.Button("LOAD"), sg.Button('SAVE'), sg.Button('Exit')]
]
window = sg.Window('User Settings Demo', layout, size=(715, 200))
# Event Loop
while True:
   event, values = window.read()
   if event in (sg.WIN_CLOSED, 'Exit'):
      break
   if event == 'LOAD':
      f = open("settings.txt", 'r')
      settings = json.load(f)
      window['-USER-'].update(value=settings['-USER-'])
      window['-ID-'].update(value=settings['-ID-'])
      window['-ROLE-'].update(value=settings['-ROLE-'])
   if event == 'SAVE':
      settings = {'-USER-': values['-USER-'],
      '-ID-': values['-ID-'],
      '-ROLE-': values['-ROLE-']}
      f = open("settings.txt", 'w')
      json.dump(settings, f)
      f.close()
window.close()

Enter the data in the input boxes and click the "Save" button.

PysimpleGUI Settings

A JSON file will be saved. To load the previously saved settings, click the "Load" button.

Advertisements