m_strat.gui.gui_helpers

  1import tkinter as tk
  2from tkinter import filedialog
  3
  4import dearpygui.dearpygui as dpg
  5
  6
  7def tk_ask_directory(fd_params: dict, callback: callable = None):
  8    """Uses tkinter filedialog to open OS native folder dialog and get a path
  9    Args:
 10        fd_params (dict): The parameters to be parsed to askdirectory
 11        callback (callable, optional): Method to do on complete. Defaults to None.
 12    Returns:
 13        str: the path that was given by askdirectory (can be None)
 14    """
 15    root = tk.Tk()
 16    root.attributes("-topmost", True)
 17    root.withdraw()
 18    path = filedialog.askdirectory(**fd_params)
 19    if callback:
 20        callback(path)
 21    return path
 22
 23
 24def tk_ask_open_file(fd_params, callback: callable = None):
 25    """Uses tkinter file dialog to open OS native file dialog and get a path
 26    Args:
 27        fd_params (dict): The parameters to be parsed to askopenfilename
 28        callback (callable, optional): Method to do on complete. Defaults to None.
 29    Returns:
 30        str: the file path that was given by askopenfilename (can be None)
 31    """
 32    root = tk.Tk()
 33    root.attributes("-topmost", True)
 34    root.withdraw()
 35    path = filedialog.askopenfilename(**fd_params)
 36    if callback and path:
 37        callback(path)
 38    return path
 39
 40
 41def tk_ask_save_file(fd_params, callback: callable = None):
 42    """Uses tkinter file dialog to determine save location
 43    Args:
 44        fd_params (dict): The parameters to be parsed to asksaveasfilenam
 45        callback (callable, optional): Method to do on complete. Defaults to None.
 46    Returns:
 47        str: the file path that was given by asksaveasfilenam (can be Non)
 48    """
 49    root = tk.Tk()
 50    root.attributes("-topmost", True)
 51    root.withdraw()
 52    path = filedialog.asksaveasfilename(**fd_params)
 53    if callback and path:
 54        callback(path)
 55    return path
 56
 57
 58def dialog_for_user_input(
 59    header: str,
 60    msg: str,
 61    val_type: type,
 62    on_confirm: callable,
 63    on_cancel: callable = None,
 64):
 65    """Method to get dpg to open a pop up, asking for a specific input value from the user
 66    Args:
 67        header (str): The title text of the pop up
 68        msg (str): The message/body text
 69        val_type (type): The type of value expected
 70        on_confirm (callable): Mandatory method to be executed once user has confirmed input
 71        on_cancel (callable, optional): Optional method to be executed if the user cancels. Defaults to None.
 72    """
 73    window_tag = dpg.generate_uuid()
 74    input_tag = dpg.generate_uuid()
 75
 76    def internal_on_confirm(sender, app_data, user_data):
 77        val = dpg.get_value(input_tag)
 78        dpg.delete_item(window_tag)
 79        if on_confirm:
 80            on_confirm(val)
 81
 82    def internal_on_cancel(sender, app_data, user_data):
 83        dpg.delete_item(window_tag)
 84        if on_cancel:
 85            on_cancel()
 86
 87    vp_width = dpg.get_viewport_client_width()
 88    vp_height = dpg.get_viewport_client_height()
 89    win_width = 350
 90    win_height = 200
 91    pos_center = (
 92        max((vp_width - win_width) // 2, 0),
 93        max((vp_height - win_height) // 2, 0),
 94    )
 95    with dpg.window(
 96        label=header,
 97        modal=True,
 98        show=True,
 99        tag=window_tag,
100        no_close=True,
101        width=win_width,
102        height=win_height,
103        pos=pos_center,
104    ):
105        dpg.add_text(msg, wrap=win_width - 20)
106        if val_type == str:
107            dpg.add_input_text(
108                tag=input_tag,
109                width=-1,
110                on_enter=True,
111                callback=internal_on_confirm,
112            )
113        elif val_type == int:
114            dpg.add_input_int(
115                tag=input_tag, width=-1, on_enter=True, callback=internal_on_confirm
116            )
117        elif val_type == float:
118            dpg.add_input_float(
119                tag=input_tag, width=-1, on_enter=True, callback=internal_on_confirm
120            )
121        else:
122            dpg.add_input_text(tag=input_tag, width=-1)
123        dpg.add_spacer(height=8)
124
125        with dpg.group(horizontal=True):
126            dpg.add_button(label="Confirm", width=100, callback=internal_on_confirm)
127            dpg.add_button(label="Cancel", width=100, callback=internal_on_cancel)
def tk_ask_directory(fd_params: dict, callback: <built-in function callable> = None):
 8def tk_ask_directory(fd_params: dict, callback: callable = None):
 9    """Uses tkinter filedialog to open OS native folder dialog and get a path
10    Args:
11        fd_params (dict): The parameters to be parsed to askdirectory
12        callback (callable, optional): Method to do on complete. Defaults to None.
13    Returns:
14        str: the path that was given by askdirectory (can be None)
15    """
16    root = tk.Tk()
17    root.attributes("-topmost", True)
18    root.withdraw()
19    path = filedialog.askdirectory(**fd_params)
20    if callback:
21        callback(path)
22    return path

Uses tkinter filedialog to open OS native folder dialog and get a path Args: fd_params (dict): The parameters to be parsed to askdirectory callback (callable, optional): Method to do on complete. Defaults to None. Returns: str: the path that was given by askdirectory (can be None)

def tk_ask_open_file(fd_params, callback: <built-in function callable> = None):
25def tk_ask_open_file(fd_params, callback: callable = None):
26    """Uses tkinter file dialog to open OS native file dialog and get a path
27    Args:
28        fd_params (dict): The parameters to be parsed to askopenfilename
29        callback (callable, optional): Method to do on complete. Defaults to None.
30    Returns:
31        str: the file path that was given by askopenfilename (can be None)
32    """
33    root = tk.Tk()
34    root.attributes("-topmost", True)
35    root.withdraw()
36    path = filedialog.askopenfilename(**fd_params)
37    if callback and path:
38        callback(path)
39    return path

Uses tkinter file dialog to open OS native file dialog and get a path Args: fd_params (dict): The parameters to be parsed to askopenfilename callback (callable, optional): Method to do on complete. Defaults to None. Returns: str: the file path that was given by askopenfilename (can be None)

def tk_ask_save_file(fd_params, callback: <built-in function callable> = None):
42def tk_ask_save_file(fd_params, callback: callable = None):
43    """Uses tkinter file dialog to determine save location
44    Args:
45        fd_params (dict): The parameters to be parsed to asksaveasfilenam
46        callback (callable, optional): Method to do on complete. Defaults to None.
47    Returns:
48        str: the file path that was given by asksaveasfilenam (can be Non)
49    """
50    root = tk.Tk()
51    root.attributes("-topmost", True)
52    root.withdraw()
53    path = filedialog.asksaveasfilename(**fd_params)
54    if callback and path:
55        callback(path)
56    return path

Uses tkinter file dialog to determine save location Args: fd_params (dict): The parameters to be parsed to asksaveasfilenam callback (callable, optional): Method to do on complete. Defaults to None. Returns: str: the file path that was given by asksaveasfilenam (can be Non)

def dialog_for_user_input( header: str, msg: str, val_type: type, on_confirm: <built-in function callable>, on_cancel: <built-in function callable> = None):
 59def dialog_for_user_input(
 60    header: str,
 61    msg: str,
 62    val_type: type,
 63    on_confirm: callable,
 64    on_cancel: callable = None,
 65):
 66    """Method to get dpg to open a pop up, asking for a specific input value from the user
 67    Args:
 68        header (str): The title text of the pop up
 69        msg (str): The message/body text
 70        val_type (type): The type of value expected
 71        on_confirm (callable): Mandatory method to be executed once user has confirmed input
 72        on_cancel (callable, optional): Optional method to be executed if the user cancels. Defaults to None.
 73    """
 74    window_tag = dpg.generate_uuid()
 75    input_tag = dpg.generate_uuid()
 76
 77    def internal_on_confirm(sender, app_data, user_data):
 78        val = dpg.get_value(input_tag)
 79        dpg.delete_item(window_tag)
 80        if on_confirm:
 81            on_confirm(val)
 82
 83    def internal_on_cancel(sender, app_data, user_data):
 84        dpg.delete_item(window_tag)
 85        if on_cancel:
 86            on_cancel()
 87
 88    vp_width = dpg.get_viewport_client_width()
 89    vp_height = dpg.get_viewport_client_height()
 90    win_width = 350
 91    win_height = 200
 92    pos_center = (
 93        max((vp_width - win_width) // 2, 0),
 94        max((vp_height - win_height) // 2, 0),
 95    )
 96    with dpg.window(
 97        label=header,
 98        modal=True,
 99        show=True,
100        tag=window_tag,
101        no_close=True,
102        width=win_width,
103        height=win_height,
104        pos=pos_center,
105    ):
106        dpg.add_text(msg, wrap=win_width - 20)
107        if val_type == str:
108            dpg.add_input_text(
109                tag=input_tag,
110                width=-1,
111                on_enter=True,
112                callback=internal_on_confirm,
113            )
114        elif val_type == int:
115            dpg.add_input_int(
116                tag=input_tag, width=-1, on_enter=True, callback=internal_on_confirm
117            )
118        elif val_type == float:
119            dpg.add_input_float(
120                tag=input_tag, width=-1, on_enter=True, callback=internal_on_confirm
121            )
122        else:
123            dpg.add_input_text(tag=input_tag, width=-1)
124        dpg.add_spacer(height=8)
125
126        with dpg.group(horizontal=True):
127            dpg.add_button(label="Confirm", width=100, callback=internal_on_confirm)
128            dpg.add_button(label="Cancel", width=100, callback=internal_on_cancel)

Method to get dpg to open a pop up, asking for a specific input value from the user Args: header (str): The title text of the pop up msg (str): The message/body text val_type (type): The type of value expected on_confirm (callable): Mandatory method to be executed once user has confirmed input on_cancel (callable, optional): Optional method to be executed if the user cancels. Defaults to None.