@@ -1208,10 +1208,12 @@ def select_widget(self, widget):
1208
1208
"""
1209
1209
Select a widget from the Menu.
1210
1210
1211
- :param widget: Widget to be selected
1212
- :type widget: :py:class:`pygame_menu.widgets.core.widget.Widget`
1211
+ :param widget: Widget to be selected or Widget ID
1212
+ :type widget: :py:class:`pygame_menu.widgets.core.widget.Widget`, str
1213
1213
:return: None
1214
1214
"""
1215
+ if isinstance (widget , str ):
1216
+ widget = self .get_widget (widget_id = widget )
1215
1217
assert isinstance (widget , _widgets .core .Widget )
1216
1218
if not widget .is_selectable :
1217
1219
raise ValueError ('widget is not selectable' )
@@ -1228,10 +1230,12 @@ def remove_widget(self, widget):
1228
1230
"""
1229
1231
Remove a widget from the Menu.
1230
1232
1231
- :param widget: Widget object
1232
- :type widget: :py:class:`pygame_menu.widgets.core.widget.Widget`
1233
+ :param widget: Widget object or ID
1234
+ :type widget: :py:class:`pygame_menu.widgets.core.widget.Widget`, str
1233
1235
:return: None
1234
1236
"""
1237
+ if isinstance (widget , str ):
1238
+ widget = self .get_widget (widget_id = widget )
1235
1239
assert isinstance (widget , _widgets .core .Widget )
1236
1240
try :
1237
1241
index = self ._widgets .index (widget ) # If not exists this raises ValueError
@@ -1253,19 +1257,19 @@ def _update_after_remove_or_hidden(self, index, update_surface=True):
1253
1257
:return: None
1254
1258
"""
1255
1259
# Check if there's more selectable widgets
1256
- nselect = 0
1260
+ n_select = 0
1257
1261
last_selectable = 0
1258
1262
for indx in range (len (self ._widgets )):
1259
1263
wid = self ._widgets [indx ] # type: _widgets.core.Widget
1260
1264
if wid .is_selectable and wid .visible :
1261
- nselect += 1
1265
+ n_select += 1
1262
1266
last_selectable = indx
1263
1267
1264
- if nselect == 0 :
1268
+ if n_select == 0 :
1265
1269
self ._index = - 1 # Any widget is selected
1266
- elif nselect == 1 :
1270
+ elif n_select == 1 :
1267
1271
self ._select (last_selectable ) # Select the unique selectable option
1268
- elif nselect > 1 :
1272
+ elif n_select > 1 :
1269
1273
if index == - 1 : # Index was hidden
1270
1274
self ._select (self ._index + 1 )
1271
1275
elif self ._index > index : # If the selected widget was after this
@@ -1400,27 +1404,27 @@ def _update_widget_position(self):
1400
1404
x_coord += self ._widget_offset [0 ]
1401
1405
1402
1406
# Calculate Y position
1403
- ysum = 1 # Compute the total height from the current row position to the top of the column
1407
+ y_sum = 1 # Compute the total height from the current row position to the top of the column
1404
1408
for r in range (row ):
1405
- rwidget = self ._widgets [int (self ._rows * col + r )] # type: _widgets.core.Widget
1406
- if rwidget .visible :
1407
- ysum += widget_rects [rwidget .get_id ()].height # Height
1408
- ysum += rwidget .get_margin ()[1 ] # Vertical margin (bottom)
1409
+ r_widget = self ._widgets [int (self ._rows * col + r )] # type: _widgets.core.Widget
1410
+ if r_widget .visible :
1411
+ y_sum += widget_rects [r_widget .get_id ()].height # Height
1412
+ y_sum += r_widget .get_margin ()[1 ] # Vertical margin (bottom)
1409
1413
1410
1414
# If no widget is before add the selection effect
1411
- yselh = rwidget .get_selection_effect ().get_margin ()[0 ]
1412
- if r == 0 and self ._widget_offset [1 ] <= yselh :
1413
- if rwidget .is_selectable :
1414
- ysum += yselh - self ._widget_offset [1 ]
1415
+ y_sel_h = r_widget .get_selection_effect ().get_margin ()[0 ]
1416
+ if r == 0 and self ._widget_offset [1 ] <= y_sel_h :
1417
+ if r_widget .is_selectable :
1418
+ y_sum += y_sel_h - self ._widget_offset [1 ]
1415
1419
1416
1420
# If the widget offset is zero, then add the selection effect to the height
1417
1421
# of the widget to avoid visual glitches
1418
- yselh = widget .get_selection_effect ().get_margin ()[0 ]
1419
- if ysum == 1 and self ._widget_offset [1 ] <= yselh : # No widget is before
1422
+ y_sel_h = widget .get_selection_effect ().get_margin ()[0 ]
1423
+ if y_sum == 1 and self ._widget_offset [1 ] <= y_sel_h : # No widget is before
1420
1424
if widget .is_selectable : # Add top margin
1421
- ysum += yselh - self ._widget_offset [1 ]
1425
+ y_sum += y_sel_h - self ._widget_offset [1 ]
1422
1426
1423
- y_coord = max (0 , self ._widget_offset [1 ]) + ysum + widget .get_padding ()[0 ]
1427
+ y_coord = max (0 , self ._widget_offset [1 ]) + y_sum + widget .get_padding ()[0 ]
1424
1428
1425
1429
# Update the position of the widget
1426
1430
widget .set_position (int (x_coord ), int (y_coord ))
@@ -1971,8 +1975,8 @@ def update(self, events):
1971
1975
1972
1976
# If mouse motion enabled, add the current mouse position to event list
1973
1977
if self ._current ._mouse and self ._current ._mouse_motion_selection :
1974
- mousex , mousey = pygame .mouse .get_pos ()
1975
- events .append (pygame .event .Event (pygame .MOUSEMOTION , {'pos' : (mousex , mousey )}))
1978
+ mouse_x , mouse_y = pygame .mouse .get_pos ()
1979
+ events .append (pygame .event .Event (pygame .MOUSEMOTION , {'pos' : (mouse_x , mouse_y )}))
1976
1980
1977
1981
for event in events : # type: pygame.event.Event
1978
1982
@@ -2245,8 +2249,8 @@ def _get_input_data(self, recursive, depth):
2245
2249
2246
2250
# Check if there is a collision between keys
2247
2251
data_keys = data .keys ()
2248
- subdata_keys = data_submenu .keys ()
2249
- for key in subdata_keys : # type: str
2252
+ sub_data_keys = data_submenu .keys ()
2253
+ for key in sub_data_keys : # type: str
2250
2254
if key in data_keys :
2251
2255
msg = 'collision between widget data ID="{0}" at depth={1}' .format (key , depth )
2252
2256
raise ValueError (msg )
0 commit comments