2020from collections import OrderedDict
2121
2222from examples_settings import Settings
23+ from openlcb .tcplink .mdnsconventions import id_from_tcp_service_name
2324
2425zeroconf_enabled = False
2526try :
@@ -90,7 +91,7 @@ class MainForm(ttk.Frame):
9091 key and the Button instance is the value.
9192 example_modules (OrderedDict[str]): The example
9293 module name is the key, and the full path is the value. If
93- examples are made modular, the value will not be nessary , but
94+ examples are made modular, the value will not be necessary , but
9495 for now just run the file in another Python instance (See
9596 run_example method).
9697
@@ -291,7 +292,10 @@ def gui(self, parent):
291292 self .row = 0
292293 self .add_field ("service_name" ,
293294 "TCP Service name (optional, sets host&port)" ,
294- gui_class = ttk .Combobox , tooltip = "" )
295+ gui_class = ttk .Combobox , tooltip = "" ,
296+ command = self .set_id_from_name ,
297+ command_text = "Copy digits to Far Node ID" )
298+ self .fields ["service_name" ].button .configure (state = tk .DISABLED )
295299 self .fields ["service_name" ].var .trace ('w' , self .on_service_name_change )
296300 self .add_field ("host" , "IP address/hostname" ,
297301 command = self .detect_hosts ,
@@ -334,8 +338,8 @@ def gui(self, parent):
334338 self .add_field (
335339 "farNodeID" , "Far Node ID" ,
336340 gui_class = ttk .Combobox ,
337- # command=self.detect_nodes, # TODO: finish detect_nodes & use
338- # command_text="Detect", # TODO: finish detect_nodes & use
341+ command = self .detect_nodes , # TODO: finish detect_nodes & use
342+ command_text = "Detect" , # TODO: finish detect_nodes & use
339343 )
340344
341345 self .add_field (
@@ -345,6 +349,17 @@ def gui(self, parent):
345349 command_text = "Default" ,
346350 )
347351
352+ self .add_field (
353+ "timeout" , "Remote nodes timeout (seconds)" ,
354+ gui_class = ttk .Entry ,
355+ )
356+
357+ self .add_field (
358+ "trace" , "Remote nodes logging" ,
359+ gui_class = ttk .Checkbutton ,
360+ text = "Trace" ,
361+ )
362+
348363 # The status widget is the only widget other than self which
349364 # is directly inside the parent widget (forces it to bottom):
350365 self .statusLabel = ttk .Label (self .parent )
@@ -362,28 +377,45 @@ def gui(self, parent):
362377 # self.rowconfigure(row, weight=1)
363378 # self.rowconfigure(self.row_count-1, weight=1) # make last row expand
364379
380+ def set_id_from_name (self ):
381+ id = self .get_id_from_name (update_button = True )
382+ if not id :
383+ return
384+ self .fields ['farNodeID' ].var .set (id )
385+
386+ def get_id_from_name (self , update_button = False ):
387+ lcc_id = id_from_tcp_service_name (self .fields ['service_name' ].var .get ())
388+ if update_button :
389+ if not lcc_id :
390+ self .fields ["service_name" ].button .configure (state = tk .DISABLED )
391+ else :
392+ self .fields ["service_name" ].button .configure (state = tk .NORMAL )
393+ return lcc_id
394+
365395 def on_service_name_change (self , index , value , op ):
366396 key = self .fields ['service_name' ].get ()
397+ _ = self .get_id_from_name (update_button = True )
367398 info = self .detected_services .get (key )
368399 if not info :
369400 # The user may be typing, so don't spam screen with messages,
370401 # just ignore incomplete entries.
371402 return
372403 # We got info, so use the info to set *other* fields:
373- self .fields ['host' ].set (info ['server' ])
404+ self .fields ['host' ].set (info ['server' ].rstrip ("." ))
405+ # ^ Remove trailing "." to prevent getaddrinfo failed.
374406 self .fields ['port' ].set (info ['port' ])
375407 self .set_status ("Hostname & Port have been set ({server}:{port})"
376408 .format (** info ))
377409
378- def add_field (self , key , text , gui_class = ttk .Entry , command = None ,
379- command_text = None , tooltip = None ):
410+ def add_field (self , key , caption , gui_class = ttk .Entry , command = None ,
411+ command_text = None , tooltip = None , text = None ):
380412 """Generate a uniform data field that may or may not affect a setting.
381413
382414 The row(s) for the data field will start at self.row, and self.row will
383415 be incremented for (each) row added by this function.
384416
385417 Args:
386- text (str): Text for the label.
418+ caption (str): Text for the label.
387419 key (str): Key to store the widget.
388420 gui_class (Misc): The ttk widget class or function to use to create
389421 the data entry widget (field.widget).
@@ -392,6 +424,8 @@ def add_field(self, key, text, gui_class=ttk.Entry, command=None,
392424 tooltip (str, optional): Add a tooltip tk.Label as field.tooltip
393425 with this text. Added even if "". Defaults to None (not added
394426 in that case).
427+ text (str, optional): Text on the input widget itself (only
428+ applies to gui_class Checkbutton).
395429 """
396430 # self.row should already be set to an empty row.
397431 self .column = 0 # Return to beginning of row
@@ -404,16 +438,27 @@ def add_field(self, key, text, gui_class=ttk.Entry, command=None,
404438 raise ValueError ("command is required for command_caption." )
405439
406440 field = DataField ()
407- field .label = ttk .Label (self , text = text )
441+ field .label = ttk .Label (self , text = caption )
408442 field .label .grid (row = self .row , column = self .column , ** self .grid_args )
409443 self .host_column = self .column
410444 self .column += 1
411445 self .fields [key ] = field
412- field .var = tk .StringVar (self .w1 )
413- field .widget = gui_class (
414- self ,
415- textvariable = field .var ,
416- )
446+ if gui_class in (ttk .Checkbutton , tk .Checkbutton ):
447+ field .var = tk .BooleanVar (self .w1 )
448+ # field.var.set(True)
449+ field .widget = gui_class (
450+ self ,
451+ # onvalue=True,
452+ # offvalue=False,
453+ variable = field .var ,
454+ text = text ,
455+ )
456+ else :
457+ field .var = tk .StringVar (self .w1 )
458+ field .widget = gui_class (
459+ self ,
460+ textvariable = field .var ,
461+ )
417462 field .widget .grid (row = self .row , column = self .column , ** self .grid_args )
418463 self .column += 1
419464
@@ -550,14 +595,14 @@ def main():
550595 window_h ,
551596 )) # WxH+X+Y format
552597 root .minsize = (window_w , window_h )
553- mainform = MainForm (root )
554- mainform .master .title ("Python OpenLCB Examples" )
598+ main_form = MainForm (root )
599+ main_form .master .title ("Python OpenLCB Examples" )
555600 try :
556- mainform .mainloop ()
601+ main_form .mainloop ()
557602 finally :
558- if mainform .zeroconf :
559- mainform .zeroconf .close ()
560- mainform .zeroconf = None
603+ if main_form .zeroconf :
604+ main_form .zeroconf .close ()
605+ main_form .zeroconf = None
561606 return 0
562607
563608
0 commit comments