made the names of variables and functions as well as the name of the class itself for Communication more generic
parent
3d62f1fabb
commit
a33dbea7c8
|
@ -1,55 +1,50 @@
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
import util
|
import util
|
||||||
|
|
||||||
# easy way to get data out of window events
|
# easy way to communicate across events
|
||||||
class Window_Interaction_Handler:
|
class Communication:
|
||||||
# constructor
|
|
||||||
def __init__(self):
|
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
|
# 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 interact(self, name, result, reverse_order=True, additional_action=None, additional_action_parameters=()):
|
def send(self, name, content, reverse_order=True):
|
||||||
if name in self.__window_interactions:
|
if name in self.__messages:
|
||||||
if reverse_order:
|
if reverse_order:
|
||||||
self.__window_interactions[name] = [result] + self.__window_interactions[name]
|
self.__messages[name] = [content] + self.__messages[name]
|
||||||
else:
|
else:
|
||||||
self.__window_interactions[name] = self.__window_interactions[name] + [result]
|
self.__messages[name] = self.__messages[name] + [content]
|
||||||
else:
|
else:
|
||||||
self.__window_interactions[name] = [result]
|
self.__messages[name] = [content]
|
||||||
|
|
||||||
if not additional_action==None:
|
# get the content of the first message tagged with name, removes the returned message by default
|
||||||
additional_action(*additional_action_parameters)
|
def get(self, name, remove=True):
|
||||||
|
if name in self.__messages and len(self.__messages[name])>0:
|
||||||
# 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:
|
|
||||||
if remove:
|
if remove:
|
||||||
result = self.__window_interactions[name].pop(0)
|
content = self.__messages[name].pop(0)
|
||||||
if len(self.__window_interactions[name])==0:
|
if len(self.__messages[name])==0:
|
||||||
del self.__window_interactions[name]
|
del self.__messages[name]
|
||||||
return result
|
return content
|
||||||
else:
|
else:
|
||||||
return self.__window_interactions[name][0]
|
return self.__messages[name][0]
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# get all results for a given event
|
# get the contents for all messages tagged with name
|
||||||
def get_results(self, name, clear=False):
|
def get_all(self, name, clear=False):
|
||||||
if name in self.__window_interactions and len(self.__window_interactions[name])>0:
|
if name in self.__messages and len(self.__messages[name])>0:
|
||||||
results = self.__window_interactions[name]
|
contents = self.__messages[name]
|
||||||
if clear:
|
if clear:
|
||||||
del self.__window_interactions[name]
|
del self.__messages[name]
|
||||||
return results
|
return contents
|
||||||
|
|
||||||
# clear results for a given event
|
# deletes all messages tagged with name
|
||||||
def clear(self, name):
|
def clear(self, name):
|
||||||
if name in self.__window_interactions:
|
if name in self.__messages:
|
||||||
del self.__window_interactions[name]
|
del self.__messages[name]
|
||||||
|
|
||||||
# destructor
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if len(self.__window_interactions)>0:
|
if len(self.__messages)>0:
|
||||||
util.warn("__window_interactions not empty upon destruction of Window_Interaction_Handler:\n"+str(self.__window_interactions))
|
util.warn("__messages not empty upon destruction of Communitation object:\n"+str(self.__messages))
|
||||||
|
|
||||||
|
|
||||||
def not_implemented():
|
def not_implemented():
|
||||||
|
|
Reference in New Issue