Pysimplegui 简明教程

PySimpleGUI - Settings

Global Settings

全局设置是可在整个应用程序范围内使用的应用程序设置。这些设置控制 Element 类用于应用于应用程序中所有元素的各种属性。

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.

这些设置以分级方式起作用。如果这些设置针对窗口提供了其他值,则全局设置将被覆盖。反过来,Window 对象中定义的设置针对特定元素提供了不同的值。

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.

例如,如果在全局范围内将字号设置为 16,则所有元素的文本都将相应显示。但是,如果布局中定义了具有 16 以外大小的字体属性的特定文本或输入元素,则它将相应更改外观。

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.

set_options 函数用于更改在全局范围内应用的设置。如果这是应用于 Windows 的设置,则该设置不仅适用于你创建的 Windows,还适用于弹出式 Windows。

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

“用户设置”是一个自动写入到硬盘的字典。用户设置存储在 Python 字典中,并保存到磁盘并从磁盘中加载。因此,各个设置是放入字典中的键。

"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()

使用词典样式的 [ ] 语法来读取一项设置。如果该项名称为 '- item-',那么,通过写入来读取值

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-']

您还可以调用 delete_entry 方法来删除该项。

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.

settings

系统将保存一个 JSON 文件。要加载之前保存的设置,请单击“加载”按钮。

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