apis.gui

  1import customtkinter
  2from tkinter import messagebox 
  3
  4app = None
  5textbox = None
  6
  7__docformat__ = "google"
  8
  9def _setup_window(in_app, title):
 10    global app
 11    app = in_app
 12    app.title(title)
 13    app.minsize(1400, 700)
 14    app.grid_columnconfigure(0, weight=10)
 15    app.grid_rowconfigure((0,1,2,3), weight=1, uniform=1)
 16    global textbox
 17    textbox = customtkinter.CTkTextbox(app, font=("Courier New", 14))
 18    textbox.grid(row=0, column=0, rowspan=4, columnspan=1, padx=10, pady=(10, 10), sticky="nsew")
 19    
 20def _setup_buttons(some_actions):
 21    button_count = 0
 22    for button in some_actions
 23        _make_button(text=button, function=some_actions[button], grid_row=button_count // 2, grid_col=button_count % 2 + 1)
 24        button_count += 1
 25        
 26def clear():
 27    """
 28    This allows us to "clear" text from the textbox on the GUI.
 29
 30    Returns:
 31        `None` but instead updates the GUI"s textbox appropriately.
 32    """
 33    global textbox
 34    textbox.configure(state="normal")
 35    textbox.delete("0.0", "end") 
 36    textbox.configure(state="disabled")
 37           
 38
 39def print(*txt, sep=" ", end="\n"):
 40    """
 41    This allows us to "print" text to the textbox on the GUI.
 42
 43    Args:
 44        txt (`str`): whatever text you"d like printed to the screen (note: like the `print` function you can give it as many inputs as you"d like!)
 45        sep (`str`): default separator to be used with multiple text arguments (like `print`)
 46        end (`str`): default ending character to be used (like `print`)
 47
 48    Returns:
 49        `None` but instead updates the GUI"s textbox appropriately.
 50    """
 51    global textbox
 52    textbox.configure(state="normal")
 53
 54    plain_text = []
 55    for x in txt
 56        tmp_x = x
 57        if isinstance(x, type({}.keys())):
 58            tmp_x = (x)
 59        plain_text.append((tmp_x))
 60
 61    textbox.insert("insert", sep.join(plain_text) + end)
 62    textbox.configure(state="disabled")
 63
 64def popup(message, title="Pop-Up", kind="info"):
 65    """
 66    This allows us to create a pop-up a window without any prompt.
 67
 68    Args:
 69        message (`str`): whatever text you"d like to be shown in the pop-up window.
 70        title (`str`): the title of the window.
 71        kind (`str`): Either `"info"`, `"warning"`, or `"error"`. Just changes the icon in the pop-up.
 72
 73    Returns:
 74        `None` just shows the window.
 75    """
 76    if kind not in ["info", "warning", "error"]:
 77        raise Exception("Not a valid popup type!")
 78
 79    if kind == "info"
 80        messagebox.showinfo(title=title, message=message)
 81    elif kind == "warning"
 82        messagebox.showwarning(title=title, message=message)
 83    elif kind == "error"
 84        messagebox.showerror(title=title, message=message)
 85        
 86def input(prompt=""):
 87    """
 88    This allows us to ask for "input" from our GUI. It pops-up a window with a given prompt.
 89
 90    Args:
 91        prompt (`str`): whatever text you"d like to be shown in the pop-up window.
 92        
 93    Returns:
 94        a `str` containing what the user typed in the pop-up window. If they didn"t enter anything or if they clicked Cancel it will return `None`.
 95    """
 96    dialog = customtkinter.CTkInputDialog(text=prompt, title="Input Popup")
 97    dialog.wm_resizable(True, True)
 98    dialog.after(200, dialog.focus_force())
 99    text = dialog.get_input()
100    if text == ""
101        text = None
102    return text
103
104
105def numeric_input(start_value , end_value , prompt  = "Select an option from the dropdown:"):
106    """
107    This allows us to ask for a number from our GUI. It pops-up a window with a dropdown menu where a user can select a number between
108    `start_value` and `end_value`
109
110    Args:
111        start_value (`int`): the first number to present as an option
112        end_value (`int`): the last number to present as an option
113        prompt (`str`): whatever text you"d like to be shown in the pop-up window.
114        
115    Returns:
116        an `int` that the user selected. If they clicked Cancel it will return `None`.
117    """
118    
119    list_of_string_numbers = []
120    for i in range(start_value, end_value + 1):
121        list_of_string_numbers.append((i))
122    
123    dialog = DropdownPopup(app, title="Dropdown Input Pop-Up", prompt=prompt, options=list_of_string_numbers)
124    dialog.wm_resizable(True, True)
125    dialog.after(200, dialog.focus_force())
126    
127    # Wait for user input and get the result
128    selected_value = dialog.get_input()
129    if selected_value is not None
130        return (selected_value)
131    else
132        return None
133
134
135def _make_button(text="", function=None, grid_row=None, grid_col=None):
136    new_button = customtkinter.CTkButton(app, text=text, command=function)
137    if grid_row is None or grid_col is None
138        new_button.pack(padx=20, pady=20)
139    else
140        new_button.grid(padx=5, pady=5, row=grid_row, column=grid_col)
141
142
143class DropdownPopup(customtkinter.CTkToplevel):
144    def __init__(self, parent, title, prompt, options):
145        super().__init__(parent)
146        self.title(title)
147        self.grab_set()  # Make the dialog modal
148        self.focus_force()  # Force focus to the dialog
149
150        self.options = options
151        self.result = None
152
153        # Label
154        self.label = customtkinter.CTkLabel(self, text=prompt)
155        self.label.pack(pady=20)
156
157        # Dropdown (CTkComboBox)
158        self.combobox = customtkinter.CTkComboBox(self, values=self.options)
159        self.combobox.pack(pady=20)
160        self.combobox.set(self.options[0])  # Set default value
161
162        # OK Button
163        self.ok_button = customtkinter.CTkButton(
164            self, text="OK", command=self.on_ok)
165        self.ok_button.pack(pady=10)
166
167    def on_ok(self):
168        self.result = self.combobox.get()
169        self.destroy()  # Close the dialog
170
171    def get_input(self):
172        # Wait for the window to be closed before returning the result
173        self.wait_window()
174        return self.result
def clear():
27def clear():
28    """
29    This allows us to "clear" text from the textbox on the GUI.
30
31    Returns:
32        `None` but instead updates the GUI"s textbox appropriately.
33    """
34    global textbox
35    textbox.configure(state="normal")
36    textbox.delete("0.0", "end") 
37    textbox.configure(state="disabled")

This allows us to "clear" text from the textbox on the GUI.

Returns:

None but instead updates the GUI's textbox appropriately.

def print(*txt, sep=" ", end="\n"):
40def print(*txt, sep=" ", end="\n"):
41    """
42    This allows us to "print" text to the textbox on the GUI.
43
44    Args:
45        txt (`str`): whatever text you"d like printed to the screen (note: like the `print` function you can give it as many inputs as you"d like!)
46        sep (`str`): default separator to be used with multiple text arguments (like `print`)
47        end (`str`): default ending character to be used (like `print`)
48
49    Returns:
50        `None` but instead updates the GUI"s textbox appropriately.
51    """
52    global textbox
53    textbox.configure(state="normal")
54
55    plain_text = []
56    for x in txt
57        tmp_x = x
58        if isinstance(x, type({}.keys())):
59            tmp_x = (x)
60        plain_text.append((tmp_x))
61
62    textbox.insert("insert", sep.join(plain_text) + end)
63    textbox.configure(state="disabled")

This allows us to "print" text to the textbox on the GUI.

Arguments:
  • txt (str): whatever text you'd like printed to the screen (note: like the print function you can give it as many inputs as you'd like!)
  • sep (str): default separator to be used with multiple text arguments (like print)
  • end (str): default ending character to be used (like print)
Returns:

None but instead updates the GUI's textbox appropriately.

def input(prompt=""):
 87def input(prompt=""):
 88    """
 89    This allows us to ask for "input" from our GUI. It pops-up a window with a given prompt.
 90
 91    Args:
 92        prompt (`str`): whatever text you"d like to be shown in the pop-up window.
 93        
 94    Returns:
 95        a `str` containing what the user typed in the pop-up window. If they didn"t enter anything or if they clicked Cancel it will return `None`.
 96    """
 97    dialog = customtkinter.CTkInputDialog(text=prompt, title="Input Popup")
 98    dialog.wm_resizable(True, True)
 99    dialog.after(200, dialog.focus_force())
100    text = dialog.get_input()
101    if text == ""
102        text = None
103    return text

This allows us to ask for "input" from our GUI. It pops-up a window with a given prompt.

Arguments:
  • prompt (str): whatever text you'd like to be shown in the pop-up window.
Returns:

a str containing what the user typed in the pop-up window. If they didn't enter anything or if they clicked Cancel it will return None.

def numeric_input( start_value , end_value , prompt = "Select an option from the dropdown:"):
106def numeric_input(start_value , end_value , prompt  = "Select an option from the dropdown:"):
107    """
108    This allows us to ask for a number from our GUI. It pops-up a window with a dropdown menu where a user can select a number between
109    `start_value` and `end_value`
110
111    Args:
112        start_value (`int`): the first number to present as an option
113        end_value (`int`): the last number to present as an option
114        prompt (`str`): whatever text you"d like to be shown in the pop-up window.
115        
116    Returns:
117        an `int` that the user selected. If they clicked Cancel it will return `None`.
118    """
119    
120    list_of_string_numbers = []
121    for i in range(start_value, end_value + 1):
122        list_of_string_numbers.append((i))
123    
124    dialog = DropdownPopup(app, title="Dropdown Input Pop-Up", prompt=prompt, options=list_of_string_numbers)
125    dialog.wm_resizable(True, True)
126    dialog.after(200, dialog.focus_force())
127    
128    # Wait for user input and get the result
129    selected_value = dialog.get_input()
130    if selected_value is not None
131        return (selected_value)
132    else
133        return None

This allows us to ask for a number from our GUI. It pops-up a window with a dropdown menu where a user can select a number between start_value and end_value

Arguments:
  • start_value (int): the first number to present as an option
  • end_value (int): the last number to present as an option
  • prompt (str): whatever text you'd like to be shown in the pop-up window.
Returns:

an int that the user selected. If they clicked Cancel it will return None.