Skip to content

Commit 0db7b4a

Browse files
committed
various
1 parent afa2c6b commit 0db7b4a

7 files changed

Lines changed: 35 additions & 25 deletions

File tree

docs/add_widgets.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def func(name):
118118
elif EXAMPLE == "CLOCK":
119119
menu = make_menu(pygame_menu.themes.THEME_DARK, "Clock")
120120

121-
clock = menu.add.clock(font_size=25, font_name=pygame_menu.font.FONT_DIGITAL)
121+
menu.add.clock(font_size=25, font_name=pygame_menu.font.FONT_DIGITAL)
122122

123123
elif EXAMPLE == "COLORINPUT":
124124
menu = make_menu(pygame_menu.themes.THEME_DARK, "Color Entry")
@@ -145,13 +145,13 @@ def func(name):
145145
elif EXAMPLE == "DROPSELECT":
146146
menu = make_menu(pygame_menu.themes.THEME_DEFAULT, "Drop Select")
147147

148-
selector_epic = menu.add.dropselect(
148+
menu.add.dropselect(
149149
title="Is pygame-menu epic?",
150150
items=[("Yes", 0), ("Absolutely Yes", 1)],
151151
font_size=16,
152152
selection_option_font_size=20,
153153
)
154-
selector_sum = menu.add.dropselect(
154+
menu.add.dropselect(
155155
title="What is the value of π?",
156156
items=[
157157
("3 (Engineer)", 0),
@@ -164,7 +164,7 @@ def func(name):
164164
selection_option_padding=(0, 5),
165165
selection_option_font_size=20,
166166
)
167-
selector_country = menu.add.dropselect(
167+
menu.add.dropselect(
168168
title="Pick a country",
169169
items=[
170170
("Argentina", "ar"),
@@ -196,7 +196,7 @@ def func(name):
196196
)
197197
menu.add.vertical_margin(75)
198198

199-
selector = menu.add.dropselect_multiple(
199+
menu.add.dropselect_multiple(
200200
title="Pick 3 colors",
201201
items=[
202202
("Black", (0, 0, 0)),
@@ -382,7 +382,7 @@ def open_link(*args) -> None:
382382
onchange=change_background_color, # User changes value with left/right keys
383383
)
384384
selector.add_self_to_kwargs() # Callbacks will receive widget as parameter
385-
selector2 = menu.add.selector(
385+
menu.add.selector(
386386
title="New color:", items=items, style=pygame_menu.widgets.SELECTOR_STYLE_FANCY
387387
)
388388

pygame_menu/_decorator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ def _update_pos_list(
11521152
try:
11531153
decoid_exists = self._coord_cache[decoid] is not None
11541154
except KeyError:
1155+
# No cached coordinates for this decoid; proceed as if unseen
11551156
pass
11561157
if (
11571158
decoid_exists

pygame_menu/widgets/widget/colorinput.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def value_changed(self) -> bool:
360360
default = "#" + default
361361
return self.get_value(as_string=True) != default
362362

363-
def get_value(self, as_string: bool = False) -> str | Tuple3IntType:
363+
def get_value(self, as_string: bool = False) -> str | float | int | Tuple3IntType:
364364
"""
365365
Return the color value as a tuple or red blue and green channels.
366366
@@ -371,7 +371,6 @@ def get_value(self, as_string: bool = False) -> str | Tuple3IntType:
371371
:param as_string: If ``True`` returns the widget value as plain text
372372
:return: Color tuple as (R, G, B) or color string
373373
"""
374-
assert isinstance(as_string, bool)
375374
if as_string:
376375
return self._input_string
377376

pygame_menu/widgets/widget/dropselect.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ def _up(self) -> None:
10891089
self._sound.play_key_add()
10901090
return None
10911091

1092-
def set_value(self, item: str | int) -> None:
1092+
def set_value(self, item: str | int, process_index: bool = False) -> None:
10931093
"""
10941094
Set the current value of the widget, selecting the item that matches the
10951095
text if ``item`` is a string, or the index if ``item`` is an integer.
@@ -1104,10 +1104,9 @@ def set_value(self, item: str | int) -> None:
11041104
11051105
This method does not trigger any event (change).
11061106
1107-
:param item: Item to select, can be a string or an integer
1107+
:param item: The item to select, either a string or an integer index.
1108+
:param process_index: Ignored in this class; used by subclasses.
11081109
"""
1109-
assert isinstance(item, (str, int)), "item must be a string or an integer"
1110-
11111110
if isinstance(item, str):
11121111
found = False
11131112
for i in self._items:
@@ -1117,13 +1116,17 @@ def set_value(self, item: str | int) -> None:
11171116
break
11181117
if not found:
11191118
raise ValueError(f'no value "{item}" found in drop select')
1119+
11201120
elif isinstance(item, int):
11211121
assert -1 <= item < len(self._items), (
11221122
"item index must be greater than zero and lower than the number "
11231123
"of items on the drop select"
11241124
)
11251125
self._index = item
11261126

1127+
# Base class ignores process_index
1128+
# (DropSelectMultiple overrides and uses it)
1129+
11271130
# Update options background selection
11281131
for b_ind_x in range(len(self._option_buttons)):
11291132
btn = self._option_buttons[b_ind_x]

pygame_menu/widgets/widget/dropselect_multiple.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,6 @@ def set_value(self, item: str | int, process_index: bool = False) -> None:
490490
:param item: Item to select, can be a string or an integer
491491
:param process_index: Adds/Removes the index from the selected indices list
492492
"""
493-
assert isinstance(item, (str, int)), "item must be a string or an integer"
494-
495493
if isinstance(item, str):
496494
found = False
497495
for i in self._items:

pygame_menu/widgets/widget/table.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ def add_row(
432432
try:
433433
cell.get_menu().remove_widget(cell)
434434
except ValueError:
435+
# Removal can fail if the cell is no longer registered in the menu
435436
pass
436437

437438
# Check the cell frame is None
@@ -866,6 +867,7 @@ def update_cell_style(
866867
try:
867868
cell.update_font({"color": font_color, "name": font})
868869
except AssertionError:
870+
# Font update may fail if parameters are invalid; ignore and keep previous font
869871
pass
870872

871873
try:

pygame_menu/widgets/widget/textinput.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
NumberType,
3030
Tuple2IntType,
3131
Tuple2NumberType,
32+
Tuple3IntType,
3233
)
3334
from pygame_menu.locals import FINGERDOWN, FINGERUP, INPUT_FLOAT, INPUT_INT, INPUT_TEXT
3435
from pygame_menu.utils import (
@@ -463,26 +464,32 @@ def clear(self) -> None:
463464
self._delete()
464465
self.change()
465466

466-
def get_value(self) -> str:
467+
def get_value(self, as_string: bool = False) -> str | float | int | Tuple3IntType:
467468
"""
468469
Return the value of the text.
469470
470-
:return: Text inside the widget
471+
:param as_string: If True, return the raw input string
472+
:return: Text inside the widget, or converted number
471473
"""
472-
value = ""
474+
if as_string:
475+
return self._input_string
476+
473477
if self._input_type == INPUT_TEXT:
474-
value = self._input_string # Without filters
475-
elif self._input_type == INPUT_FLOAT:
478+
return self._input_string
479+
480+
if self._input_type == INPUT_FLOAT:
476481
try:
477-
value = float(self._input_string)
482+
return float(self._input_string)
478483
except ValueError:
479-
value = 0
480-
elif self._input_type == INPUT_INT:
484+
return 0
485+
486+
if self._input_type == INPUT_INT:
481487
try:
482-
value = int(float(self._input_string))
488+
return int(float(self._input_string))
483489
except ValueError:
484-
value = 0
485-
return value
490+
return 0
491+
492+
return self._input_string
486493

487494
def scale(self, *args, **kwargs) -> TextInput:
488495
raise WidgetTransformationNotImplemented()

0 commit comments

Comments
 (0)