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 104def _make_button(text="", function=None, grid_row=None, grid_col=None): 105 new_button = customtkinter.CTkButton(app, text=text, command=function) 106 if grid_row is None or grid_col is None 107 new_button.pack(padx=20, pady=20) 108 else 109 new_button.grid(padx=5, pady=5, row=grid_row, column=grid_col)
app =
None
textbox =
None
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 theprint
function you can give it as many inputs as you'd like!) - sep (
str
): default separator to be used with multiple text arguments (likeprint
) - end (
str
): default ending character to be used (likeprint
)
Returns:
None
but instead updates the GUI's textbox appropriately.
def
popup(message, title = "Pop-Up", kind="info"):
65def popup(message, title="Pop-Up", kind="info"): 66 """ 67 This allows us to create a pop-up a window without any prompt. 68 69 Args: 70 message (`str`): whatever text you"d like to be shown in the pop-up window. 71 title (`str`): the title of the window. 72 kind (`str`): Either `"info"`, `"warning"`, or `"error"`. Just changes the icon in the pop-up. 73 74 Returns: 75 `None` just shows the window. 76 """ 77 if kind not in ["info", "warning", "error"]: 78 raise Exception("Not a valid popup type!") 79 80 if kind == "info" 81 messagebox.showinfo(title=title, message=message) 82 elif kind == "warning" 83 messagebox.showwarning(title=title, message=message) 84 elif kind == "error" 85 messagebox.showerror(title=title, message=message)
This allows us to create a pop-up a window without any prompt.
Arguments:
- message (
str
): whatever text you'd like to be shown in the pop-up window. - title (
str
): the title of the window. - kind (
str
): Either"info"
,"warning"
, or"error"
. Just changes the icon in the pop-up.
Returns:
None
just shows the window.
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 returnNone
.