added Window_Interaction_Handler and a simple dialog on startup

Window_Interaction_Handler can be used as a bus system. It is designed to keep track of events until they are being read out somewhere.
The dialog on startup checks whether a config file exists and prompts to create one if it doesn’t.
master
BodgeMaster 2022-02-14 13:04:51 +01:00
parent 3a66d4e2d7
commit 02a6f555e0
1 changed files with 99 additions and 0 deletions

99
main.py Normal file
View File

@ -0,0 +1,99 @@
#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
import sys, os, json, traceback
def warn(text):
print("WARNING: "+str(text), file=sys.stderr)
traceback.print_stack()
# easy way to get data out of window events
class Window_Interaction_Handler:
# constructor
def __init__(self):
self.__window_interactions = {}
# 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:
if reverse_order:
self.__window_interactions[name] = [result] + self.__window_interactions[name]
else:
self.__window_interactions[name] = self.__window_interactions[name] + [result]
else:
self.__window_interactions[name] = [result]
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:
if remove:
result = self.__window_interactions[name].pop(0)
if len(self.__window_interactions[name])==0:
del self.__window_interactions[name]
return result
else:
return self.__window_interactions[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]
if clear:
del self.__window_interactions[name]
return results
# clear results for a given event
def clear(self, name):
if name in self.__window_interactions:
del self.__window_interactions[name]
# destructor
def __del__(self):
if len(self.__window_interactions)>0:
warn("__window_interactions not empty upon destruction of Window_Interaction_Handler:\n"+str(self.__window_interactions))
# read configuration
home_directory = os.path.expanduser("~")
config_file_path = os.path.join(home_directory, "some_ide_config.json")
configuration = {}
if os.path.isfile(config_file_path):
try:
config_file = open(config_file_path, "r")
configuration = json.loads(config_file.read())
config_file.close()
except:
#TODO: show an error message
warn("Not implemented!")
else:
# config not found
dialog_interaction_handler = Window_Interaction_Handler()
dialog = tk.Tk()
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) #TODO: add functionality
ttk.Button(buttons_frame, text="Quit", command=lambda: dialog_interaction_handler.interact("create", False, additional_action=dialog.destroy)).grid(column=1, row=0) #TODO: add functionality
dialog.resizable(0,0)
dialog.mainloop()
if dialog_interaction_handler.get_result("create"):
#TODO: store default configuration
warn("Not implemented!")
else:
print("No config present and user chose not to create one. Exiting.")
sys.exit()
#window = tk.Tk()
#frame = ttk.Frame(window, padding=10)
#frame.grid()
#ttk.Label(frame, text="Hello, World!").grid(column=0, row=0)
#ttk.Button(frame, text="Quit", command=window.destroy).grid(column=0, row=1)
#window.mainloop()