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.
master
BodgeMaster 2022-03-19 11:42:31 +01:00
parent 80e735937c
commit ae6fe2896c
2 changed files with 14 additions and 8 deletions

View File

@ -1,7 +1,8 @@
import os, sys, json import os, sys, json
#TODO: port to QT once that little mainloop issue has been resolved...
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
import gui_helper, util import util
class Config: class Config:
def __init__(self, file_path, default_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) util.error("An exception occurred while trying to load the configuration.", handle_gracefully=False)
else: else:
# config not found # config not found
dialog_interaction_handler = gui_helper.Window_Interaction_Handler() dialog_communication = util.Communication()
dialog = tk.Tk() dialog = tk.Tk()
dialog.title("No configuration found") dialog.title("No configuration found")
ttk.Label(dialog, text="No configuration found!").pack() ttk.Label(dialog, text="No configuration found!").pack()
buttons_frame = tk.Frame(dialog) buttons_frame = tk.Frame(dialog)
buttons_frame.pack() 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="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_interaction_handler.interact("create", False, additional_action=dialog.destroy)).grid(column=1, 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.resizable(0,0)
dialog.mainloop() dialog.mainloop()
if dialog_interaction_handler.get_result("create"): if dialog_communication.get("create"):
self.__current_config = default_config self.__current_config = default_config
try: try:
config_file = open(self.__file_path, "w") config_file = open(self.__file_path, "w")

11
util.py
View File

@ -27,12 +27,13 @@ def error(message, is_exception=True, handle_gracefully=True):
sys.exit(EXIT_ERROR) sys.exit(EXIT_ERROR)
# easy way to communicate across events # easy way to communicate across events
#TODO: make thread safe
class Communication: class Communication:
def __init__(self): def __init__(self):
self.__messages = {} 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 # 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): def send(self, name, content, reverse_order=True, additional_action=None):
if name in self.__messages: if name in self.__messages:
if reverse_order: if reverse_order:
self.__messages[name] = [content] + self.__messages[name] self.__messages[name] = [content] + self.__messages[name]
@ -40,6 +41,10 @@ class Communication:
self.__messages[name] = self.__messages[name] + [content] self.__messages[name] = self.__messages[name] + [content]
else: else:
self.__messages[name] = [content] 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 # get the content of the first message tagged with name, removes the returned message by default
def get(self, name, remove=True): def get(self, name, remove=True):
@ -69,4 +74,4 @@ class Communication:
def __del__(self): def __del__(self):
if len(self.__messages)>0: 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))