@@ -224,7 +224,9 @@ def menuconfig(kconf):
224
224
225
225
# Select the first item and focus the Treeview, so that keyboard controls
226
226
# work immediately
227
- _select (_tree , _tree .get_children ()[0 ])
227
+ children = _tree .get_children ()
228
+ if children :
229
+ _select (_tree , children [0 ])
228
230
_tree .focus_set ()
229
231
230
232
# Make geometry information available for centering the window. This
@@ -1249,15 +1251,18 @@ def _set_val(sc, val):
1249
1251
# rare cases, but is fast and flicker-free.
1250
1252
1251
1253
stayput = _loc_ref_item () # Item to preserve scroll for
1252
- old_row = _item_row (stayput )
1254
+ if stayput :
1255
+ old_row = _item_row (stayput )
1253
1256
1254
- _update_tree ()
1257
+ _update_tree ()
1255
1258
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 ()
1261
1266
1262
1267
if _jump_to_tree :
1263
1268
_update_jump_to_display ()
@@ -1630,7 +1635,12 @@ def _toggle_tree_mode(_):
1630
1635
def _do_tree_mode ():
1631
1636
# Updates the UI for the current tree mode (full-tree or single-menu)
1632
1637
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 ]
1634
1644
1635
1645
if not _single_menu :
1636
1646
# _jump_to() -> _enter_menu() already updates the tree, but
@@ -1647,7 +1657,9 @@ def _enter_menu_and_select_first(menu):
1647
1657
# mode.
1648
1658
1649
1659
_enter_menu (menu )
1650
- _select (_tree , _tree .get_children ()[0 ])
1660
+ children = _tree .get_children ()
1661
+ if children :
1662
+ _select (_tree , children [0 ])
1651
1663
1652
1664
1653
1665
def _enter_menu (menu ):
@@ -1700,7 +1712,11 @@ def _loc_ref_item():
1700
1712
1701
1713
# Otherwise, use the middle item on the screen. If it doesn't exist, the
1702
1714
# 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
1704
1720
1705
1721
1706
1722
def _vis_loc_ref_item ():
@@ -1807,7 +1823,10 @@ def _try_save(save_fn, filename, description):
1807
1823
messagebox .showerror (
1808
1824
"Error saving " + description ,
1809
1825
"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 ),
1811
1830
),
1812
1831
)
1813
1832
return False
@@ -1829,7 +1848,7 @@ def _try_load(filename):
1829
1848
messagebox .showerror (
1830
1849
"Error loading configuration" ,
1831
1850
"Error loading '{}': {} (errno: {})" .format (
1832
- filename , e .strerror , errno .errorcode [ e .errno ]
1851
+ filename , e .strerror , errno .errorcode . get ( e .errno , e . errno )
1833
1852
),
1834
1853
)
1835
1854
return False
0 commit comments