From ae6fe2896c92a41fa764123ebcf5296c17cdcaab Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sat, 19 Mar 2022 11:42:31 +0100 Subject: [PATCH] use util.Communication instead of gui_helper.Window_Interaction_Handler Communication and Window_Interaction_Handler are the same class but I renamed things to be more generic and moved it to util. --- config.py | 11 ++++++----- util.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/config.py b/config.py index 0bd5c37..590c16a 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,8 @@ import os, sys, json +#TODO: port to QT once that little mainloop issue has been resolved... import tkinter as tk from tkinter import ttk -import gui_helper, util +import util class Config: def __init__(self, file_path, default_config): @@ -18,19 +19,19 @@ class Config: util.error("An exception occurred while trying to load the configuration.", handle_gracefully=False) else: # config not found - dialog_interaction_handler = gui_helper.Window_Interaction_Handler() + dialog_communication = util.Communication() dialog = tk.Tk() dialog.title("No configuration found") ttk.Label(dialog, text="No configuration found!").pack() buttons_frame = tk.Frame(dialog) buttons_frame.pack() - ttk.Button(buttons_frame, text="Create", command=lambda: dialog_interaction_handler.interact("create", True, additional_action=dialog.destroy)).grid(column=0, row=0) - ttk.Button(buttons_frame, text="Quit", command=lambda: dialog_interaction_handler.interact("create", False, additional_action=dialog.destroy)).grid(column=1, row=0) + ttk.Button(buttons_frame, text="Create", command=lambda: dialog_communication.send("create", True, additional_action=dialog.destroy)).grid(column=0, row=0) + ttk.Button(buttons_frame, text="Quit", command=lambda: dialog_communication.send("create", False, additional_action=dialog.destroy)).grid(column=1, row=0) dialog.resizable(0,0) dialog.mainloop() - if dialog_interaction_handler.get_result("create"): + if dialog_communication.get("create"): self.__current_config = default_config try: config_file = open(self.__file_path, "w") diff --git a/util.py b/util.py index dad201f..1b80b05 100644 --- a/util.py +++ b/util.py @@ -27,12 +27,13 @@ def error(message, is_exception=True, handle_gracefully=True): sys.exit(EXIT_ERROR) # easy way to communicate across events +#TODO: make thread safe class Communication: def __init__(self): self.__messages = {} - # send a message tagged with name and containing content, adds to the beginning of the list of messages with the same tag by default - def send(self, name, content, reverse_order=True): + # send a message tagged with name and containing content, adds to the beginning of the list of messages with the same tag by default, a function to run as an additional action can be provided + def send(self, name, content, reverse_order=True, additional_action=None): if name in self.__messages: if reverse_order: self.__messages[name] = [content] + self.__messages[name] @@ -40,6 +41,10 @@ class Communication: self.__messages[name] = self.__messages[name] + [content] else: self.__messages[name] = [content] + if additional_action == None: + pass + else: + additional_action() # get the content of the first message tagged with name, removes the returned message by default def get(self, name, remove=True): @@ -69,4 +74,4 @@ class Communication: def __del__(self): if len(self.__messages)>0: - warn("__messages not empty upon destruction of Communitation object:\n"+str(self.__messages)) + warn("__messages not empty upon destruction of Communication object:\n"+str(self.__messages))