# Main file of the program. Run this file to start the application. # import packages: from os.path import exists import traceback from tkinter import messagebox # import other project files: import cage_func as func from User_Interface import HelmholtzGUI from User_Interface import ui_print import User_Interface as ui import globals as g import config_handling as config import csv_logging as log def program_end(): # called on exception or when user closes application # safely shuts everything down and saves any unsaved data g.exitFlag = True # tell everything else the application has been closed if g.app is not None: # the main Tkinter app object has been initialized before if g.app.pages[ui.ExecuteCSVMode].csv_thread is not None: # check if a thread for executing CSVs exists g.app.pages[ui.ExecuteCSVMode].csv_thread.stop() # stop the thread func.shut_down_all() # shut down devices if log.unsaved_data: # Check if there is logged data that has not been saved yet # open pop-up to ask user if he wants to save the data: save_log = messagebox.askquestion("Save log data?", "There seems to be unsaved logging data. " "Do you wish to write it to a file now?") if save_log == 'yes': # user has chosen yes filepath = log.select_file() # let user select a file to write to log.write_to_file(log.log_data, filepath) # write the data to the chosen file if g.app is not None: g.app.destroy() # close application try: # start normal operations config.CONFIG_FILE = 'config.ini' # set the config file path # ToDo: remember what the last config file was if not exists(config.CONFIG_FILE): # config file does not exist yet print("Config file not found, creating new from defaults.") config.reset_config_to_default() # create configuration object from defaults config.write_config_to_file(config.CONFIG_OBJECT) # write the configuration object to a new file config.CONFIG_OBJECT = config.get_config_from_file(config.CONFIG_FILE) # read configuration data from config file print("Starting setup...") func.setup_all() # initiate communication with devices and initialize all major program objects print("\nOpening User Interface...") g.app = HelmholtzGUI() # initialize user interface g.exitFlag = False # tell all functions that the user interface is now running g.app.state('zoomed') # open UI in maximized window g.app.StatusDisplay.continuous_label_update(g.app, 500) # initiate regular Status Display updates (ms) ui_print("Program Initialized") config.check_config(config.CONFIG_OBJECT) # check config for values exceeding limits ui_print("\nStarting setup...") # do setup again, so it is printed in the UI console ToDo: do it only once func.setup_all() # initiate communication with devices and initialize all major program objects g.app.protocol("WM_DELETE_WINDOW", program_end) # call program_end function if user closes the application g.app.mainloop() # start main program loop except Exception as e: # An error has occurred somewhere in the program print("\nAn error occurred, Shutting down.") # shop pup-up error message: message = "%s.\nSee python console traceback for more details. " \ "\nShutting down devices, check equipment to confirm." % e messagebox.showerror("Error!", message) print(traceback.print_exc()) # print error traceback in the python console program_end() # safely close everything and shut down devices