Skip to content

Commit

Permalink
GUI: Re-enable main window correctly when closing secondary windows (#…
Browse files Browse the repository at this point in the history
…415)

* Re-enable main window widgets when X button is pressed on advanced options and Metadata entry windows

* Refactor _BasicModal
  • Loading branch information
sdatkinson authored May 14, 2024
1 parent 215ea64 commit 5d185ba
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions nam/train/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,35 @@ class _CheckboxKeys(Enum):
IGNORE_DATA_CHECKS = "ignore_data_checks"


class _TopLevelWithOk(tk.Toplevel):
"""
Toplevel with an Ok button (provide yourself!)
"""

def __init__(
self, on_ok: Callable[[None], None], resume_main: Callable[[None], None]
):
"""
:param on_ok: What to do when "Ok" button is pressed
"""
super().__init__()
self._on_ok = on_ok
self._resume_main = resume_main

def destroy(self, pressed_ok: bool = False):
if pressed_ok:
self._on_ok()
self._resume_main()
super().destroy()


class _BasicModal(object):
"""
Message and OK button
"""

def __init__(self, resume_main, msg: str):
self._root = tk.Toplevel()
self._root = _TopLevelWithOk((lambda: None), resume_main)
self._text = tk.Label(self._root, text=msg)
self._text.pack()
self._ok = tk.Button(
Expand All @@ -271,14 +293,9 @@ def __init__(self, resume_main, msg: str):
width=_BUTTON_WIDTH,
height=_BUTTON_HEIGHT,
fg="black",
command=self._close,
command=lambda: self._root.destroy(pressed_ok=True),
)
self._ok.pack()
self._resume_main = resume_main

def _close(self):
self._root.destroy()
self._resume_main()


class _GUIWidgets(Enum):
Expand Down Expand Up @@ -693,9 +710,8 @@ class _AdvancedOptionsGUI(object):
"""

def __init__(self, resume_main, parent: _GUI):
self._resume_main = resume_main
self._parent = parent
self._root = tk.Toplevel()
self._root = _TopLevelWithOk(self._apply, resume_main)
self._root.title("Advanced Options")

# Architecture: radio buttons
Expand Down Expand Up @@ -749,11 +765,11 @@ def __init__(self, resume_main, parent: _GUI):
width=_BUTTON_WIDTH,
height=_BUTTON_HEIGHT,
fg="black",
command=self._apply_and_destroy,
command=lambda: self._root.destroy(pressed_ok=True),
)
self._button_ok.pack()

def _apply_and_destroy(self):
def _apply(self):
"""
Set values to parent and destroy this object
"""
Expand All @@ -772,18 +788,15 @@ def _apply_and_destroy(self):
self._parent.advanced_options.threshold_esr = (
None if threshold_esr == "null" else threshold_esr
)
self._root.destroy()
self._resume_main()


class _UserMetadataGUI(object):
# Things that are auto-filled:
# Model date
# gain
def __init__(self, resume_main, parent: _GUI):
self._resume_main = resume_main
self._parent = parent
self._root = tk.Tk()
self._root = _TopLevelWithOk(self._apply, resume_main)
self._root.title("Metadata")

LabeledText = partial(_LabeledText, right_width=_METADATA_RIGHT_WIDTH)
Expand Down Expand Up @@ -852,11 +865,11 @@ def __init__(self, resume_main, parent: _GUI):
width=_BUTTON_WIDTH,
height=_BUTTON_HEIGHT,
fg="black",
command=self._apply_and_destroy,
command=lambda: self._root.destroy(pressed_ok=True),
)
self._button_ok.pack()

def _apply_and_destroy(self):
def _apply(self):
"""
Set values to parent and destroy this object
"""
Expand All @@ -868,9 +881,6 @@ def _apply_and_destroy(self):
self._parent.user_metadata.tone_type = self._tone_type.get()
self._parent.user_metadata_flag = True

self._root.destroy()
self._resume_main()


def _install_error():
window = tk.Tk()
Expand Down

0 comments on commit 5d185ba

Please sign in to comment.