@@ -224,7 +224,9 @@ def menuconfig(kconf):
224224
225225 # Select the first item and focus the Treeview, so that keyboard controls
226226 # work immediately
227- _select (_tree , _tree .get_children ()[0 ])
227+ children = _tree .get_children ()
228+ if children :
229+ _select (_tree , children [0 ])
228230 _tree .focus_set ()
229231
230232 # Make geometry information available for centering the window. This
@@ -1249,15 +1251,18 @@ def _set_val(sc, val):
12491251 # rare cases, but is fast and flicker-free.
12501252
12511253 stayput = _loc_ref_item () # Item to preserve scroll for
1252- old_row = _item_row (stayput )
1254+ if stayput :
1255+ old_row = _item_row (stayput )
12531256
1254- _update_tree ()
1257+ _update_tree ()
12551258
1256- # If the reference item disappeared (can happen if the change was done
1257- # from the jump-to dialog), then avoid messing with the scroll and hope
1258- # for the best
1259- if _attached (stayput ):
1260- _tree .yview_scroll (_item_row (stayput ) - old_row , "units" )
1259+ # If the reference item disappeared (can happen if the change was done
1260+ # from the jump-to dialog), then avoid messing with the scroll and hope
1261+ # for the best
1262+ if _attached (stayput ):
1263+ _tree .yview_scroll (_item_row (stayput ) - old_row , "units" )
1264+ else :
1265+ _update_tree ()
12611266
12621267 if _jump_to_tree :
12631268 _update_jump_to_display ()
@@ -1630,7 +1635,12 @@ def _toggle_tree_mode(_):
16301635def _do_tree_mode ():
16311636 # Updates the UI for the current tree mode (full-tree or single-menu)
16321637
1633- loc_ref_node = _id_to_node [_loc_ref_item ()]
1638+ loc_ref = _loc_ref_item ()
1639+ if not loc_ref :
1640+ # Tree is empty (should not happen in normal use)
1641+ return
1642+
1643+ loc_ref_node = _id_to_node [loc_ref ]
16341644
16351645 if not _single_menu :
16361646 # _jump_to() -> _enter_menu() already updates the tree, but
@@ -1647,7 +1657,9 @@ def _enter_menu_and_select_first(menu):
16471657 # mode.
16481658
16491659 _enter_menu (menu )
1650- _select (_tree , _tree .get_children ()[0 ])
1660+ children = _tree .get_children ()
1661+ if children :
1662+ _select (_tree , children [0 ])
16511663
16521664
16531665def _enter_menu (menu ):
@@ -1700,7 +1712,11 @@ def _loc_ref_item():
17001712
17011713 # Otherwise, use the middle item on the screen. If it doesn't exist, the
17021714 # tree is probably really small, so use the first item in the entire tree.
1703- return _tree .identify_row (_tree .winfo_height () // 2 ) or _tree .get_children ()[0 ]
1715+ middle_item = _tree .identify_row (_tree .winfo_height () // 2 )
1716+ if middle_item :
1717+ return middle_item
1718+ children = _tree .get_children ()
1719+ return children [0 ] if children else None
17041720
17051721
17061722def _vis_loc_ref_item ():
@@ -1807,7 +1823,10 @@ def _try_save(save_fn, filename, description):
18071823 messagebox .showerror (
18081824 "Error saving " + description ,
18091825 "Error saving {} to '{}': {} (errno: {})" .format (
1810- description , e .filename , e .strerror , errno .errorcode [e .errno ]
1826+ description ,
1827+ e .filename ,
1828+ e .strerror ,
1829+ errno .errorcode .get (e .errno , e .errno ),
18111830 ),
18121831 )
18131832 return False
@@ -1829,7 +1848,7 @@ def _try_load(filename):
18291848 messagebox .showerror (
18301849 "Error loading configuration" ,
18311850 "Error loading '{}': {} (errno: {})" .format (
1832- filename , e .strerror , errno .errorcode [ e .errno ]
1851+ filename , e .strerror , errno .errorcode . get ( e .errno , e . errno )
18331852 ),
18341853 )
18351854 return False
0 commit comments