diff --git a/DnD_Time_Manager.py b/DnD_Time_Manager.py index ac7e663..b2fa05a 100644 --- a/DnD_Time_Manager.py +++ b/DnD_Time_Manager.py @@ -14,6 +14,7 @@ from sys import exit from os import remove from shutil import copyfile, copytree, rmtree +import aux_functions as aux VERSION="v0.9.0" @@ -278,7 +279,7 @@ def debug_log(text): [sg.Text("Time Adjustment"), sg.InputText("0", size=(5,1), key="hour_input", tooltip="Hour Change"), sg.InputText("0", size=(5,1), key="day_input", tooltip="Day Change"), sg.Button("Submit"), sg.VerticalSeparator(color="gray", ), sg.Button("End Session")], - [sg.InputText(size=(40,1), key="log_input", tooltip="Log Input"), sg.Button("Log"), sg.VerticalSeparator(color="gray"), sg.Button("Open Log")] + [sg.InputText(size=(40,1), key="log_input", tooltip="Log Input"), sg.Button("Log"), sg.VerticalSeparator(color="gray"), sg.Button("Open Log")], ] updatable=["hour_display", "day_display", "tenday_display", "month_display", "year_display"]+["temp_display", "precip_display"]+["WS_display", "WD_display"] @@ -287,18 +288,24 @@ def debug_log(text): window_title="D&D Time Manager - "+campaign else: window_title="DEV - D&D Time Manager - "+campaign + print("-----------------------------------") window=sg.Window(window_title, main_layout, finalize=True, icon="dnd_logo.ico", return_keyboard_events=True) print("-----------------------------------") +size=window.size +position=window.current_location() # Event loop------------------------------------------------------------------- while True: - position=window.current_location() + event, values = window.read() + # print(event) focused_enter=None - + if event!=None: + position=window.current_location() + centre=aux.window_centre(position, size) if event in ('\r', QT_ENTER_KEY1, QT_ENTER_KEY2): @@ -310,7 +317,6 @@ def debug_log(text): if event == sg.WIN_CLOSED: #breaks loop if window is closed - # position=0 break elif event=="Log" or focused_enter=="log": #submits log @@ -375,7 +381,9 @@ def debug_log(text): print(i[0]) if time_comparison(db.time_data(),i[1]): to_remove.append(i) - popup.alert_box(text=i[0], window_name="Reminder", theme=pref["theme"], position=position) + window.disable() + popup.alert_box(text=i[0], window_name="Reminder", theme=pref["theme"], par_centre=centre) + window.enable() for i in to_remove: print("removing "+i[0]) db.reminders.remove(i) @@ -389,7 +397,9 @@ def debug_log(text): # File -------------------------------------------------------------------- elif event.endswith("::new_campaign"): old_campaign=campaign - campaign=popup.create_campaign(user_area, first=False, theme=pref["theme"], position=position) + window.disable() + campaign=popup.create_campaign(user_area, first=False, theme=pref["theme"], par_centre=centre) + window.enable() if campaign!=None: print(campaign) @@ -464,8 +474,10 @@ def debug_log(text): elif event.endswith("::rename_campaign"): + window.disable() + new_campaign=popup.rename_window(campaign, theme=pref["theme"], par_centre=centre) + window.enable() - new_campaign=popup.rename_window(campaign, theme=pref["theme"], position=position) if new_campaign!=None: try: mkdir(user_area+r"/campaigns/{}".format(new_campaign)) @@ -473,10 +485,14 @@ def debug_log(text): file_type=file.split(".")[-1] rename(r"{}/{}".format(camp_dir,file), user_area+r"/campaigns/{}/{}.{}".format(new_campaign,new_campaign,file_type)) except FileExistsError as e: - popup.alert_box(text="Capaign \"{}\" already exists".format(new_campaign), theme=pref["theme"], position=position) + window.disable() + popup.alert_box(text="Capaign \"{}\" already exists".format(new_campaign), theme=pref["theme"], par_centre=centre) + window.enable() except Exception as e: - popup.alert_box(text="Unable to rename campaign", theme=pref["theme"], position=position) + window.disable() + popup.alert_box(text="Unable to rename campaign", theme=pref["theme"], par_centre=centre) print(e) + window.enable() else: rmdir(camp_dir) old_campaign=campaign @@ -489,15 +505,19 @@ def debug_log(text): update_menu() pickler(user_area+"/pref.pkl", pref) - popup.alert_box(text="Rename sucessful", sound=False, window_name="Rename", theme=pref["theme"], position=position) - + window.disable() + popup.alert_box(text="Rename sucessful", sound=False, window_name="Rename", theme=pref["theme"], par_centre=centre) + window.enable() elif event.endswith("::delete_campaign"): - if popup.choice_box(text="Are you sure you want to delete campaign \"{}\"?".format(campaign), window_name="Delete Campaign", theme=pref["theme"], position=position)==True: + if popup.choice_box(text="Are you sure you want to delete campaign \"{}\"?".format(campaign), window_name="Delete Campaign", theme=pref["theme"], par_centre=centre)==True: send2trash(camp_dir) pref["last campaign"].remove(campaign) - popup.alert_box(text="Campaign deleted", sound=False, window_name="Delete Campaign", theme=pref["theme"], position=position) + window.disable() + popup.alert_box(text="Campaign deleted", sound=False, window_name="Delete Campaign", theme=pref["theme"], par_centre=centre) + window.enable() + if len(listdir(user_area+"/campaigns"))!=0: #loads most recent possible campaign for i in pref["last campaign"]: if i in listdir(user_area+"/campaigns") and exists(user_area+"/campaigns/{}/{}.pkl".format(i, i)): @@ -510,8 +530,10 @@ def debug_log(text): campaign=file print(campaign) if len(listdir(user_area+"/campaigns"))==0 or campaign==None: - campaign=popup.create_campaign(user_area, first=True, theme=pref["theme"], position=position) - + window.disable() + campaign=popup.create_campaign(user_area, first=True, theme=pref["theme"], par_centre=centre) + window.enable() + pref["last campaign"].insert(0, campaign) pref["last campaign"]=list(dict.fromkeys(pref["last campaign"])) @@ -547,7 +569,10 @@ def debug_log(text): elif event.endswith("::preferences"): - new_pref, save, db = popup.pref_window(pref, db, theme=pref["theme"], position=position) + window.disable() + new_pref, save, db = popup.pref_window(pref, db, theme=pref["theme"], par_centre=centre) + window.enable() + if save==True: pref=new_pref pickler(user_area+"/pref.pkl", pref) @@ -561,12 +586,17 @@ def debug_log(text): elif event.endswith("::raw_time_out"): print(db.day_raw,db.hour) - popup.alert_box(text="{} hours, {} days\n(This value can be accessed from the error log)".format(db.hour,db.day_raw), window_name="Raw Time", sound=False, theme=pref["theme"], position=position) + window.disable() + popup.alert_box(text="{} hours, {} days\n(This value can be accessed from the error log)".format(db.hour,db.day_raw), window_name="Raw Time", sound=False, theme=pref["theme"], par_centre=centre) + window.enable() error("Raw time request - {} hours, {} days".format(db.hour,db.day_raw), sound=False) elif event.endswith("::set_reminder"): time_data=(window["hour_display"].get(), window["day_display"].get(), window["month_display"].get(), window["year_display"].get() ) - remind_data,pref=popup.set_reminder(time_data, pref, theme=pref["theme"], position=position) + window.disable() + remind_data,pref=popup.set_reminder(time_data, pref, theme=pref["theme"], par_centre=centre) + window.enable() + if remind_data !=False: db.reminders.append(remind_data) pickler(camp_dir+"/"+campaign+".pkl", db) @@ -575,11 +605,13 @@ def debug_log(text): elif event.endswith("::view_reminders"): time_data=(window["hour_display"].get(), window["day_display"].get(), window["month_display"].get(), window["year_display"].get() ) - if popup.view_reminders(db, time_data, theme=pref["theme"], position=position)=="set_reminder": - remind_data, pref=popup.set_reminder(time_data, pref, theme=pref["theme"], position=position) + window.disable() + if popup.view_reminders(db, time_data, theme=pref["theme"], par_centre=centre)=="set_reminder": + remind_data, pref=popup.set_reminder(time_data, pref, theme=pref["theme"], par_centre=centre) if remind_data !=False: db.reminders.append(remind_data) print(db.reminders) + window.enable() pickler(camp_dir+"/"+campaign+".pkl", db) # Help--------------------------------------------------------------------- @@ -589,8 +621,9 @@ def debug_log(text): about_text="D&D Time Manager\nVersion: {}".format(VERSION) else: about_text="D&D Time Manager\nVersion: {} - DEV".format(VERSION) - popup.alert_box(text=about_text, window_name="About", button_text="Close", sound=False, theme=pref["theme"], position=position) - + window.disable() + popup.alert_box(text=about_text, window_name="About", button_text="Close", sound=False, theme=pref["theme"], par_centre=centre) + window.enable() elif event.endswith("::readme"): try: @@ -606,7 +639,10 @@ def debug_log(text): _=urlopen("https://github.com/") except: - popup.alert_box(text="Unable to reach github.com", theme=pref["theme"], position=position) + window.disable() + popup.alert_box(text="Unable to reach github.com", theme=pref["theme"], par_centre=centre) + window.enable() + else: startfile("https://github.com/JP-Carr/DnD_Time_Manager") @@ -630,7 +666,9 @@ def debug_log(text): except FileNotFoundError: pref["last campaign"].remove(event) update_menu() - popup.alert_box("Unable to load campaign \"{}\"".format(event), theme=pref["theme"], position=position) + window.disable() + popup.alert_box("Unable to load campaign \"{}\"".format(event), theme=pref["theme"], par_centre=centre) + window.enable() except Exception as e: error(e) @@ -648,14 +686,16 @@ def debug_log(text): update_menu() pickler(user_area+"/pref.pkl", pref) - + # else: - # print (event) + # print (event) + window.bring_to_front() # End of loop------------------------------------------------------------------ if pref["end_session_on_close"]==True and DEV_MODE==False: end_session() - + +#pref["window_position"]=position pref["theme"]=pref["new_theme"] pickler(user_area+"/pref.pkl", pref) diff --git a/window_layouts.py b/window_layouts.py index 9510ce1..bcc98ed 100644 --- a/window_layouts.py +++ b/window_layouts.py @@ -1,5 +1,5 @@ import PySimpleGUI as sg -#from time import sleep +from time import sleep from os import listdir, mkdir from os.path import abspath from database_class import db, pickler @@ -8,21 +8,36 @@ import condition_lists from database_class import time_increment from error import error +import aux_functions as aux +from copy import deepcopy QT_ENTER_KEY1 = 'special 16777220' QT_ENTER_KEY2 = 'special 16777221' icon_path="dnd_logo.ico" -def alert_box(text="TEXT HERE", window_name="ALERT", button_text="OK", sound=True, theme=None, position=(None,None)): +def window_centre(window,par_centre): + size=window.size + x,y=aux.TL_from_centre(par_centre, size) + if x<0: + x=0 + if y<0: + y=0 + event, values = window.read(timeout = 0) + window.move(int(x),int(y)) + window.reappear() + +def alert_box(text="TEXT HERE", window_name="ALERT", button_text="OK", sound=True, theme=None, par_centre=(None,None)): lines=text.split("\n") sg.theme(theme) layout=[[sg.Text(" "+lines[i]+" ")] for i in range(len(lines))]+[ - # [sg.Text(" "+text+" ")], [sg.Button(button_text)] ] - window=sg.Window(window_name, layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=True, return_keyboard_events=True, location=position) + + window=sg.Window(window_name, layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=True, return_keyboard_events=True, alpha_channel=0) + window_centre(window,par_centre) + if sound==True: print("\a") @@ -36,13 +51,14 @@ def alert_box(text="TEXT HERE", window_name="ALERT", button_text="OK", sound=Tru return True -def choice_box(text, window_name="", theme=None, position=(None,None)): +def choice_box(text, window_name="", theme=None, par_centre=(None,None)): sg.theme(theme) layout=[ [sg.Text(text)], [sg.Button("Yes"), sg.Button("No")] ] - window=sg.Window(window_name, layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=True, return_keyboard_events=True, location=position) + window=sg.Window(window_name, layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=True, return_keyboard_events=True, alpha_channel=0) + window_centre(window,par_centre) print("\a") while True: @@ -57,7 +73,7 @@ def choice_box(text, window_name="", theme=None, position=(None,None)): window.close() return True -def create_campaign(user_area, first=False, theme=None, position=(None,None)): +def create_campaign(user_area, first=False, theme=None, par_centre=(None,None)): sg.theme(theme) layout=[ [sg.Text("New Campaign")], @@ -65,8 +81,9 @@ def create_campaign(user_area, first=False, theme=None, position=(None,None)): [sg.Text("Name"), sg.InputText("", size=(25,1), key="campaign_name")], [sg.Button("Create")] ] - window=sg.Window("New...", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, return_keyboard_events=True, location=position) - + window=sg.Window("New...", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, return_keyboard_events=True, alpha_channel=0) + window_centre(window,par_centre) + while True: event, values = window.read() focused_enter=None @@ -92,7 +109,7 @@ def create_campaign(user_area, first=False, theme=None, position=(None,None)): try: if name in listdir(abspath(user_area+"/campaigns")): - alert_box(text="Campaign \"{}\" already exists".format(name), theme=theme, position=position) + alert_box(text="Campaign \"{}\" already exists".format(name), theme=theme, par_centre=par_centre) pass else: _dir=abspath(user_area+"/campaigns/{}".format(name)) @@ -105,10 +122,10 @@ def create_campaign(user_area, first=False, theme=None, position=(None,None)): return name except Exception as e: error(e) - alert_box(text="\"{}\" is not a valid campaign name".format(name), theme=theme, position=position) + alert_box(text="\"{}\" is not a valid campaign name".format(name), theme=theme, par_centre=par_centre) pass -def pref_window(pref, db, theme=None, position=(None,None)): +def pref_window(pref, db, theme=None, par_centre=(None,None)): sg.theme(theme) @@ -147,8 +164,9 @@ def pref_window(pref, db, theme=None, position=(None,None)): ], [sg.Button("Save"), sg.Button("Cancel")], ] - window=sg.Window("Preferences", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, location=position) - + window=sg.Window("Preferences", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, alpha_channel=0) + window_centre(window,par_centre) + while True: event, values = window.read() if event == sg.WIN_CLOSED or event=="Cancel": @@ -160,7 +178,7 @@ def pref_window(pref, db, theme=None, position=(None,None)): try: _=int(window["session_num"].get()) except ValueError: - alert_box(text="Please enter a valid session number", theme=theme, position=position) + alert_box(text="Please enter a valid session number", theme=theme, par_centre=par_centre) window["session_num"].Update(str(db.session_num)) continue @@ -178,7 +196,7 @@ def pref_window(pref, db, theme=None, position=(None,None)): return pref, save, db -def rename_window(old_name, theme=None, position=(None,None)): +def rename_window(old_name, theme=None, par_centre=(None,None)): sg.theme(theme) layout=[ [sg.Text("Rename Campaign - "+old_name)], @@ -187,8 +205,9 @@ def rename_window(old_name, theme=None, position=(None,None)): [sg.Button("Confirm"), sg.Button("Cancel")] ] - window=sg.Window("Rename", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, return_keyboard_events=True, location=position) - + window=sg.Window("Rename", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, return_keyboard_events=True, alpha_channel=0) + window_centre(window,par_centre) + while True: event, values = window.read() @@ -212,11 +231,11 @@ def rename_window(old_name, theme=None, position=(None,None)): return name else: if name in listdir(): - alert_box(text="Campaign \"{}\" already exists".format(name), theme=theme, position=position) + alert_box(text="Campaign \"{}\" already exists".format(name), theme=theme, par_centre=par_centre) pass -def set_reminder(time_data, pref, theme=None, position=(None,None)): +def set_reminder(time_data, pref, theme=None, par_centre=(None,None)): sg.theme(theme) hour, day, month, year=time_data @@ -237,8 +256,9 @@ def set_reminder(time_data, pref, theme=None, position=(None,None)): [sg.Button("Confirm"), sg.Button("Cancel")], ] - window=sg.Window("Set Reminder", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True, disable_minimize=False, return_keyboard_events=True, location=position) - + window=sg.Window("Set Reminder", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True, disable_minimize=False, return_keyboard_events=True, alpha_channel=0) + window_centre(window,par_centre) + if radio_date==True: for j in ("hour_input","day_input","month_input","year_input"): window[j].update(disabled=True) @@ -295,7 +315,7 @@ def set_reminder(time_data, pref, theme=None, position=(None,None)): try: _=int(i) except ValueError: - alert_box(text="Please enter integer values", theme=theme, position=position) + alert_box(text="Please enter integer values", theme=theme, par_centre=par_centre) continue @@ -310,11 +330,11 @@ def set_reminder(time_data, pref, theme=None, position=(None,None)): # else: # print(event) -def view_reminders(db, time_data, theme=None, position=(None,None)): +def view_reminders(db, time_data, theme=None, par_centre=(None,None)): sg.theme(theme) if db.reminders==[]: - if choice_box("No reminders found. Would you like to create one?", window_name="Alert", theme=theme, position=position)==True: + if choice_box("No reminders found. Would you like to create one?", window_name="Alert", theme=theme, par_centre=par_centre)==True: return "set_reminder" else: return @@ -340,8 +360,9 @@ def view_reminders(db, time_data, theme=None, position=(None,None)): [sg.Button("Delete", disabled=True)] ] - window=sg.Window("View Reminders", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, location=position) - + window=sg.Window("View Reminders", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, alpha_channel=0) + window_centre(window,par_centre) + while True: event, values = window.read() if event == sg.WIN_CLOSED: @@ -368,7 +389,7 @@ def view_reminders(db, time_data, theme=None, position=(None,None)): window.close() -def test_window(theme=None, position=(None,None)): +def test_window(theme=None, par_centre=(None,None)): sg.theme(theme) # c1=[sg.Button("Confirm"), sg.Button("Cancel")] layout=[ @@ -377,18 +398,27 @@ def test_window(theme=None, position=(None,None)): [sg.Text("New name"), sg.InputText("", size=(25,1), key="campaign_name")], #[sg.Button("Confirm"), sg.Button("Cancel")], [sg.Listbox(["aaaaa","bbbbb","ccccc","ddddd","eeeee","fffff","ggggg","hhhhh"], size=(15,6), key="list")], - [sg.Button("x")] + [sg.Button("x"),sg.Button("y")] ] - window=sg.Window("Preferences", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, location=position) - + window=sg.Window("Preferences", layout, finalize=True, icon=icon_path, element_justification="center", force_toplevel=True,disable_minimize=False, alpha_channel=0) + window_centre(window,par_centre) + #event, values = window.read(timeout = 0) + window.move(0,0) + #sleep(1) + # window.reappear() + i,j=0,0 while True: + event, values = window.read() if event == sg.WIN_CLOSED: break elif event=="x": - print( window["tab"].read()) + window.move(i,j) + i+=20 + j+=20 + window.close() if __name__=="__main__": - test_window(position=(-10,-5)) \ No newline at end of file + test_window(par_centre=(-10,-5)) \ No newline at end of file