-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings_example_2.py
More file actions
101 lines (82 loc) · 3.12 KB
/
settings_example_2.py
File metadata and controls
101 lines (82 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import sys
import pyguiadapterlite
from pyguiadapterlite import (
FnExecuteWindow,
Action,
SettingsWindow,
SettingsWindowConfig,
Menu,
GUIAdapter,
uprint,
FnExecuteWindowConfig,
JsonSettingsBase,
)
from pyguiadapterlite.types import StringValue, IntValue, RangedIntValue
APP_SETTINGS_FILE = "settings.json"
class AppSettings(JsonSettingsBase):
url = StringValue(label="Remote URL", default_value="https://www.example.com")
port = IntValue(label="Port Number", default_value=8080)
username = StringValue(label="Username", default_value="admin")
password = StringValue(label="Password", default_value="", echo_char="*")
timeout = RangedIntValue(
label="Timeout", default_value=10, min_value=1, max_value=60
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
def load_settings() -> AppSettings:
try:
return AppSettings.load(file_path=APP_SETTINGS_FILE, encoding="utf-8")
except BaseException as e:
print(f"Error loading settings: {e}", file=sys.stderr)
return AppSettings()
def save_settings(settings: AppSettings):
try:
settings.save(file_path=APP_SETTINGS_FILE, encoding="utf-8")
except BaseException as e:
print(f"Error saving settings: {e}", file=sys.stderr)
app_settings = load_settings()
def my_app_function(request: str):
# Here we can access the settings fields using dot notation
uprint(f"Remote URL: {app_settings.url}")
uprint(f"Port Number: {app_settings.port}")
uprint(f"Username: {app_settings.username}")
uprint(f"Password: {app_settings.password}")
uprint(f"Timeout: {app_settings.timeout}")
uprint(f"Request: {request}")
if __name__ == "__main__":
def on_action_show_settings(window: FnExecuteWindow, action: Action):
_ = action # Unused
window.show_sub_window(
SettingsWindow,
settings=app_settings,
modal=True,
config=SettingsWindowConfig(
title="Application Settings",
content_title="Connection Options",
confirm_button_text="Save Changes",
cancel_button_text="Discard Changes",
allow_restore_defaults=True,
restore_defaults_button_text="Restore Defaults",
),
)
# save the settings here
save_settings(app_settings)
file_menu = Menu(
title="File",
actions=[Action(text="Settings", on_triggered=on_action_show_settings)],
)
pyguiadapterlite.set_locale_code("en_US") # set language
adapter = GUIAdapter(dpi_aware=True) # enable high DPI support
adapter.add(
my_app_function,
window_menus=[file_menu],
window_config=FnExecuteWindowConfig(
title="My App", # set the title of the window
print_function_result=False, # don't print the result of the function to the console of the window
show_function_result=False, # don't show the result of the function in a message box
),
)
adapter.run()
# app exited here
# save the settings here
save_settings(app_settings)