Skip to content
Snippets Groups Projects
Commit 17ad08ee authored by Alexandros Asonitis's avatar Alexandros Asonitis
Browse files

TLM-CTLM programming part 5(ini files)

parent 374296ca
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@ import os
from datetime import datetime
import pandas as pd
import configparser
# Interface to collect information about the sample
def sample_interface():
......@@ -300,3 +301,38 @@ def choose_folder():
root.destroy()
return folder
#save parameters as ini
def save_as_ini(default_filename):
root = tk.Tk()
root.withdraw()
root.lift() #show window above all other applications
root.attributes("-topmost", True)#window stays above all other applications
file = filedialog.asksaveasfilename(defaultextension=".ini", filetypes=[("Ini files","*.ini")],title = "save as ini",initialfile =default_filename)
#check if the file path is correct(.txt)
while file.endswith(".ini") == False:
#open again filedialog with error message box
tk.messagebox.showerror(message='invalid filename!')
file = filedialog.asksaveasfilename(defaultextension=".ini", filetypes=[("Ini files","*.ini")],title = "save as ini",initialfile =default_filename)
root.destroy()
return file
# load ini file
def load_ini():
root = tk.Tk()
root.withdraw()
root.lift() #show window above all other applications
root.attributes("-topmost", True)#window stays above all other applications
file = filedialog.askopenfilename(filetypes=[("Ini files","*.ini")],title ='Select ini file')
while file.endswith(".ini") == False:
#open again filedialog with error message box
tk.messagebox.showerror(message='invalid filename!')
file = filedialog.askopenfilename(filetypes=[("Ini files","*.ini")],title = "Select ini file")
root.destroy()
return file
......@@ -232,9 +232,87 @@ def on_exit_button_clicked(b):
else:
change_state(first_time_buttons)
def on_export_ini_clicked(b):
with output:
#disable all widgets
disable_widgets(input_fields)
disable_widgets(all_buttons)
#open file
type = sample["type"].value
config = configparser.ConfigParser()
default_filename = f'{type}.ini'
file = save_as_ini(default_filename)
with open(file,'w') as configfile:
config.add_section('ALL VALUES ARE IN THE UNITS WRITTEN IN THE INTERFACE')
config.add_section('IT IS RECOMMENDED TO CHANGE THE INI FILE FROM THE INTERFACE AND DO NOT CHANGE ANY VALUES MANUALLY')
config.add_section("Sample Information")
for information,widget in sample.items():
#remove field,sample_series, processing number
if information =="field" or information == "sample_series" or information =="processing_number":
continue
config.set("Sample Information",information,str(widget.value))
config.add_section("Measurement Parameters")
for parameter,widget in parameters.items():
config.set("Measurement Parameters",parameter,str(widget.value))
config.write(configfile)
enable_widgets(input_fields)
enable_widgets(all_buttons)
disable_widgets(first_time_buttons)
def on_import_ini_clicked(b):
with output:
#disable all widgets
disable_widgets(input_fields)
disable_widgets(all_buttons)
#load values to the interface
config = configparser.ConfigParser()
file = load_ini()
try:
config.read(file)
#load sample information
for information,widget in sample.items():
if information =="field" or information == "sample_series" or information =="processing_number":
continue
widget.value = config.get('Sample Information',information)
#load measurement parameters
for parameter,widget in parameters.items():
widget.value = config.get('Measurement Parameters',parameter)
information_box("all parameters loaded succesfully")
except Exception as error:
if type(error).__name__ =='NoSectionError':
information_box(f"{error}.Explanation: Section(header) [section] does not exist. Create a new ini file or compare it with functional ini files!")
elif type(error).__name__=='NoOptionError':
information_box(f'{error}.Explanation: The variable name before the equal sign is not recognized. Create a new ini file or compare it with functional ini files!')
elif type(error).__name__ == 'TraitError':
information_box(f'{error}.Explanation: Invalid Parameter Setting. Check if you set an invalid value!')
elif type(error).__name__ =="DuplicateOptionError":
information_box(f"{error}. Explaination: The section contains the setted parameter more than once!")
else:
information_box(f"A {type(error).__name__} has occurred. Create A new ini file")
enable_widgets(input_fields)
enable_widgets(all_buttons)
disable_widgets(first_time_buttons)
start_button.on_click(on_start_button_clicked)
continue_button.on_click(on_continue_button_clicked)
repeat_button.on_click(on_repeat_button_clicked)
exit_button.on_click(on_exit_button_clicked)
export_ini_button.on_click(on_export_ini_clicked)
import_ini_button.on_click(on_import_ini_clicked)
\ No newline at end of file
......@@ -9,7 +9,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c4927643bdd241a986420e6a0cfe3e5a",
"model_id": "332bd840c3994f1c9a4176d0026e3a0d",
"version_major": 2,
"version_minor": 0
},
......@@ -30,7 +30,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2e3a2b26b98949e7ac131a4c8f7d7027",
"model_id": "08f62f8ba6394b25a1ef905b37ef303c",
"version_major": 2,
"version_minor": 0
},
......@@ -44,7 +44,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "be86978739cc49c48962a277f8a82c78",
"model_id": "1658c8c1c7ba48bd98ae6e3ac8310693",
"version_major": 2,
"version_minor": 0
},
......@@ -66,7 +66,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ecfa05065df648cc8a143c1804ebec12",
"model_id": "1f4ed50826014670b17ac5736146d778",
"version_major": 2,
"version_minor": 0
},
......@@ -88,7 +88,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1149d141b84346c3ada4e0b3f551e1b0",
"model_id": "a8392160c8054f6683ca2f325227ab6d",
"version_major": 2,
"version_minor": 0
},
......@@ -102,7 +102,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "96ca019415384e4bac88b47577f5010b",
"model_id": "9ddd9cca6bf8492bb6b66682bfcce130",
"version_major": 2,
"version_minor": 0
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment