Skip to content

Add sg.Text as available field element / Add edit-protecting a button #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions examples/journal_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@

layout = [
[ss.selector('Journal', sg.Table, num_rows=10, headings=headings)],
[ss.actions('Journal', edit_protect=False)],
[ss.field('Journal.entry_date')],
[ss.actions('Journal')],
[ss.field('Journal.entry_date'),
# Add a CalendarButton to your date fields
sg.CalendarButton("Select Date", close_when_date_chosen=True, target="Journal.entry_date", # <- target matches your ss.field
format="%Y-%m-%d", size=(10, 1),key='datepicker')],
[ss.field('Journal.mood_id', sg.Combo, size=(30, 10), label='My mood:', auto_size_text=False)],
[ss.field('Journal.title')],
[ss.field('Journal.entry', sg.MLine, size=(71, 20))]
Expand All @@ -72,6 +75,22 @@
# Requery the data since we made changes to the sort order
frm['Journal'].requery()

# ------------------------------------------
# How to Edit Protect your sg.CalendarButton
# ------------------------------------------

# The sg.CalendarButton was not auto-mapped during Form creation.
# We need to do it manually, using `frm.map_element`.
# Typically, to manually map an sg.Input, you would pass the sg.Element, DataSet, and column it refers to,
# but when edit-protecting a Calendar button (or any button), pass None as the column.

frm.map_element(win['datepicker'], frm['Journal'], column=None)
# ['Key of Button'] ^ must be None

# By default, action() includes an edit_protect() call, that disables edits in the window.
# You can toggle it off with:
frm.edit_protect() # toggle on/off

# ---------
# MAIN LOOP
# ---------
Expand Down Expand Up @@ -100,4 +119,5 @@
- using the label keyword argument to Form.record() to define a custom label
- using Tables as Form.selector() element types
- changing the sort order of database dataset
"""
- Edit-Protecting a sg.CalendarButton
"""
25 changes: 19 additions & 6 deletions pysimplesql/pysimplesql.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ def records_changed(self, column: str = None, recursive=True) -> bool:
dirty = False
# First check the current record to see if it's dirty
for mapped in self.frm.element_map:
# skip mapped items with no column
if mapped.column is None:
continue

# Compare the DB version to the GUI version
if mapped.table == self.table:
# if passed custom column name
Expand Down Expand Up @@ -1220,6 +1224,10 @@ def save_record(self, display_message: bool = True, update_elements: bool = True

# Propagate GUI data back to the stored current_row
for mapped in self.frm.element_map:
# skip mapped items with no column
if mapped.column is None:
continue

if mapped.dataset == self:

# convert the data into the correct data type using the domain in ColumnInfo
Expand Down Expand Up @@ -1919,9 +1927,10 @@ def map_element(self, element: sg.Element, dataset: DataSet, column: str, where_

:param element: A PySimpleGUI Element
:param dataset: A `DataSet` object
:param column: The name of the column to bind to the element
:param where_column: Used for ke, value shorthand TODO: expand on this
:param where_value: Used for ey, value shorthand TODO: expand on this
:param column: The name of the column to bind to the element.
Pass None if mapping a `sg.Button` to a `DataSet` to enable edit_protect.
:param where_column: Used for key, value shorthand TODO: expand on this
:param where_value: Used for key, value shorthand TODO: expand on this
:returns: None
"""
logger.debug(f'Mapping element {element.key}')
Expand Down Expand Up @@ -2379,6 +2388,10 @@ def update_elements(self, target_data_key: str = None, edit_protect_only: bool =
# Render GUI Elements
# d= dictionary (the element map dictionary)
for mapped in self.element_map:
# if a column-less element is added
if mapped.column is None:
continue

# If the optional target_data_key parameter was passed, we will only update elements bound to that table
if target_data_key is not None:
if mapped.table != self[target_data_key].table:
Expand Down Expand Up @@ -2473,7 +2486,7 @@ def update_elements(self, target_data_key: str = None, edit_protect_only: bool =
eat_events(self.window)
continue

elif type(mapped.element) is sg.PySimpleGUI.InputText or type(mapped.element) is sg.PySimpleGUI.Multiline:
elif type(mapped.element) in [sg.PySimpleGUI.InputText, sg.PySimpleGUI.Multiline, sg.PySimpleGUI.Text]:
# Update the element in the GUI
# For text objects, lets clear it first...
mapped.element.update('') # HACK for sqlite query not making needed keys! This will blank it out
Expand Down Expand Up @@ -2674,8 +2687,8 @@ def update_element_states(self, table: str, disable: bool = None, visible: bool
if mapped.table != table:
continue
element = mapped.element
if type(element) is sg.PySimpleGUI.InputText or type(element) is sg.PySimpleGUI.MLine or type(
element) is sg.PySimpleGUI.Combo or type(element) is sg.PySimpleGUI.Checkbox:
if type(element) in [sg.PySimpleGUI.InputText, sg.PySimpleGUI.MLine, sg.PySimpleGUI.Combo,
sg.PySimpleGUI.Checkbox, sg.PySimpleGUI.Button]:
# if element.Key in self.window.key_dict.keys():
logger.debug(f'Updating element {element.Key} to disabled: {disable}, visible: {visible}')
if disable is not None:
Expand Down