3232# Standard Library Imports
3333import tkinter as tk
3434from time import sleep
35- from tkinter import filedialog
35+ from tkinter import filedialog , messagebox
3636
3737# Third Party Imports
3838
@@ -80,6 +80,7 @@ def __init__(self, root, splash_screen):
8080 )
8181
8282 self .view .top_window .add_button .config (command = self .add_microscope )
83+ self .view .top_window .new_button .config (command = self .new_configuration )
8384 self .view .top_window .load_button .config (command = self .load_configuration )
8485 self .view .top_window .save_button .config (command = self .save )
8586 self .view .top_window .cancel_button .config (command = self .on_cancel )
@@ -97,10 +98,24 @@ def on_cancel(self):
9798 exit ()
9899
99100 def add_microscope (self ):
100- """Evaluate the number of configurations and create the configuration window """
101+ """Add a new microscope tab """
101102 self .create_config_window (self .microscope_id )
102103 self .microscope_id += 1
103104
105+ def delete_microscopes (self ):
106+ """Delete all microscopes"""
107+ # delete microscopes
108+ for index in range (self .view .microscope_window .index ("end" )):
109+ tab_id = self .view .microscope_window .tabs ()[index ]
110+ self .view .microscope_window .forget (tab_id )
111+ self .view .microscope_window .tab_list = []
112+ self .microscope_id = 0
113+
114+ def new_configuration (self ):
115+ """Create new configurations"""
116+ self .delete_microscopes ()
117+ self .create_config_window (self .microscope_id )
118+
104119 def save (self ):
105120 """Save configuration file"""
106121
@@ -128,6 +143,8 @@ def set_value(temp_dict, key_list, value):
128143 )
129144 if not filename :
130145 return
146+ # warning_info
147+ warning_info = {}
131148 config_dict = {}
132149 for tab_index in self .view .microscope_window .tabs ():
133150 microscope_name = self .view .microscope_window .tab (tab_index , "text" )
@@ -168,8 +185,9 @@ def set_value(temp_dict, key_list, value):
168185 ].strip ()
169186 k = variables [k_idx ].get ()
170187 if k .strip () == "" :
188+ warning_info [hardware_name ] = True
171189 print (
172- f"Notice: { hardware_name } has an empty value! Please double check!"
190+ f"Notice: { hardware_name } has an empty value { ref } ! Please double check if it's okay !"
173191 )
174192
175193 if k_idx in value_dict :
@@ -191,11 +209,18 @@ def set_value(temp_dict, key_list, value):
191209 except tk ._tkinter .TclError :
192210 v = ""
193211 print (
194- f"Notice: { hardware_name } has an empty value! Please double check!"
212+ f"Notice: { hardware_name } has an empty value { k } ! Please double check!"
195213 )
214+ warning_info [hardware_name ] = True
196215 set_value (temp_dict , k .split ("/" ), v )
197216
198217 self .write_to_yaml (config_dict , filename )
218+ # display warning
219+ if warning_info :
220+ messagebox .showwarning (
221+ title = "Configuration" ,
222+ message = f"There are empty value(s) with { ', ' .join (warning_info .keys ())} . Please double check!"
223+ )
199224
200225 def write_to_yaml (self , config , filename ):
201226 """write yaml file
@@ -234,15 +259,8 @@ def create_config_window(self, id):
234259 tab_name = "Microscope-" + str (id )
235260 microscope_tab = MicroscopeTab (
236261 self .view .microscope_window ,
237- name = tab_name ,
238- index = id ,
239262 root = self .view .root ,
240263 )
241- setattr (
242- self .view .microscope_window ,
243- f"microscope_tab_{ id } " ,
244- microscope_tab ,
245- )
246264 self .view .microscope_window .tab_list .append (tab_name )
247265 for hardware_type , widgets in hardwares_dict .items ():
248266 if not widgets :
@@ -268,6 +286,7 @@ def load_configuration(self):
268286 """Load configuration"""
269287
270288 def get_widget_value (name , value_dict ):
289+ """Get the value from a dict"""
271290 value = value_dict
272291 for key in name .split ("/" ):
273292 if key .strip () == "" :
@@ -278,13 +297,15 @@ def get_widget_value(name, value_dict):
278297 return value
279298
280299 def get_widgets_value (widgets , value_dict ):
300+ """Get all key-value from valude_dict, keys are from widgets"""
281301 temp = {}
282302 for key in widgets :
283303 if key == "frame_config" :
284304 continue
285305 if widgets [key ][1 ] in ["Button" , "Label" ]:
286306 continue
287307 value = get_widget_value (key , value_dict )
308+ # widgets[key][3] is the value mapping dict
288309 if widgets [key ][1 ] != "Spinbox" and widgets [key ][3 ]:
289310 reverse_value_dict = dict (
290311 map (lambda v : (v [1 ], v [0 ]), widgets [key ][3 ].items ())
@@ -295,6 +316,7 @@ def get_widgets_value(widgets, value_dict):
295316 return temp
296317
297318 def build_widgets_value (widgets , value_dict ):
319+ """According to valude_dict build values for widgets"""
298320 if widgets is None or value_dict is None :
299321 return [None ]
300322 result = []
@@ -333,24 +355,27 @@ def build_widgets_value(widgets, value_dict):
333355 result .append (get_widgets_value (widgets , value_dict ))
334356
335357 return result
336-
358+ # ask file name
337359 file_name = filedialog .askopenfilename (
338360 defaultextension = ".yml" , filetypes = [("Yaml file" , "*.yml *.yaml" )]
339361 )
340362 if not file_name :
341363 return
342- # delete microscopes
343- for index in range (self .view .microscope_window .index ("end" )):
344- tab_id = self .view .microscope_window .tabs ()[index ]
345- self .view .microscope_window .forget (tab_id )
346- self .view .microscope_window .tab_list = []
347364
365+ # read configuration.yaml
348366 config_dict = load_yaml_file (file_name )
367+ if config_dict is None or "microscopes" not in config_dict :
368+ messagebox .showerror (
369+ title = "Configuration" ,
370+ message = "It's not a valid configuration.yaml file!"
371+ )
372+ return
373+
374+ self .delete_microscopes ()
375+
349376 for i , microscope_name in enumerate (config_dict ["microscopes" ].keys ()):
350377 microscope_tab = MicroscopeTab (
351378 self .view .microscope_window ,
352- name = microscope_name ,
353- index = i ,
354379 root = self .view .root ,
355380 )
356381 self .view .microscope_window .add (
@@ -359,6 +384,7 @@ def build_widgets_value(widgets, value_dict):
359384 sticky = tk .NSEW ,
360385 )
361386 self .view .microscope_window .tab_list .append (microscope_name )
387+
362388 for hardware_type , widgets in hardwares_dict .items ():
363389 hardware_ref_name = hardwares_config_name_dict [hardware_type ]
364390 # build dictionary values for widgets
0 commit comments