made the names of variables and functions as well as the name of the class itself for Communication more generic

master
BodgeMaster 2022-03-19 08:34:12 +01:00
parent 3d62f1fabb
commit a33dbea7c8
1 changed files with 28 additions and 33 deletions

View File

@ -1,55 +1,50 @@
import tkinter as tk
import util
# easy way to get data out of window events
class Window_Interaction_Handler:
# constructor
# easy way to communicate across events
class Communication:
def __init__(self):
self.__window_interactions = {}
self.__messages = {}
# add a result for an interaction event, saves results in reverse order by default, can optionally call another function
def interact(self, name, result, reverse_order=True, additional_action=None, additional_action_parameters=()):
if name in self.__window_interactions:
# 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):
if name in self.__messages:
if reverse_order:
self.__window_interactions[name] = [result] + self.__window_interactions[name]
self.__messages[name] = [content] + self.__messages[name]
else:
self.__window_interactions[name] = self.__window_interactions[name] + [result]
self.__messages[name] = self.__messages[name] + [content]
else:
self.__window_interactions[name] = [result]
self.__messages[name] = [content]
if not additional_action==None:
additional_action(*additional_action_parameters)
# get first result for a given event from the list of results (newest (default) or oldest), removes the returned result from the list by default
def get_result(self, name, remove=True):
if name in self.__window_interactions and len(self.__window_interactions[name])>0:
# get the content of the first message tagged with name, removes the returned message by default
def get(self, name, remove=True):
if name in self.__messages and len(self.__messages[name])>0:
if remove:
result = self.__window_interactions[name].pop(0)
if len(self.__window_interactions[name])==0:
del self.__window_interactions[name]
return result
content = self.__messages[name].pop(0)
if len(self.__messages[name])==0:
del self.__messages[name]
return content
else:
return self.__window_interactions[name][0]
return self.__messages[name][0]
else:
return None
# get all results for a given event
def get_results(self, name, clear=False):
if name in self.__window_interactions and len(self.__window_interactions[name])>0:
results = self.__window_interactions[name]
# get the contents for all messages tagged with name
def get_all(self, name, clear=False):
if name in self.__messages and len(self.__messages[name])>0:
contents = self.__messages[name]
if clear:
del self.__window_interactions[name]
return results
del self.__messages[name]
return contents
# clear results for a given event
# deletes all messages tagged with name
def clear(self, name):
if name in self.__window_interactions:
del self.__window_interactions[name]
if name in self.__messages:
del self.__messages[name]
# destructor
def __del__(self):
if len(self.__window_interactions)>0:
util.warn("__window_interactions not empty upon destruction of Window_Interaction_Handler:\n"+str(self.__window_interactions))
if len(self.__messages)>0:
util.warn("__messages not empty upon destruction of Communitation object:\n"+str(self.__messages))
def not_implemented():