Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions pygame_menu/examples/other/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def __init__(self) -> None:
w_deco.disable(on_layer)
widget.set_attribute("on_layer", on_layer)

def widget_select(sel: bool, wid: pygame_menu.widgets.Widget, _):
def widget_select(sel: bool, wid: pygame_menu.widgets.Widget, _) -> None:
"""
Function triggered if widget is selected
"""
Expand Down Expand Up @@ -237,9 +237,9 @@ def _operate(self) -> int | float:

:return: Operation result
"""
a = 0 if self.curr == "" else float(self.curr)
b = 0 if self.prev == "" else float(self.prev)
c = 0
a = 0.0 if self.curr == "" else float(self.curr)
b = 0.0 if self.prev == "" else float(self.prev)
c = 0.0
if self.op == "+":
c = a + b
elif self.op == "-":
Expand All @@ -259,19 +259,24 @@ def _press(self, digit: int | str) -> None:

:param digit: Number or symbol
"""
if digit in ("+", "-", "x", "/"):
if isinstance(digit, int):
digit_str = str(digit)
else:
digit_str = digit

if digit_str in ("+", "-", "x", "/"):
if self.curr != "":
if self.op != "":
self.prev = str(self._operate())
else:
self.prev = self.curr
self.curr = ""
self.op = digit
self.op = digit_str
if len(self.prev) <= 8:
self.screen.set_title(self.prev + self.op)
else:
self.screen.set_title("Ans" + self.op)
elif digit == "=":
elif digit_str == "=":
if self.prev == "":
self.curr = ""
self.screen.set_title("0")
Expand All @@ -287,12 +292,12 @@ def _press(self, digit: int | str) -> None:
else:
if self.op == "":
if len(self.prev) <= 7:
self.prev += str(digit)
self.prev += digit_str
self.prev = self._format(self.prev)
self.screen.set_title(self.prev)
else:
if len(self.curr) <= 7:
self.curr += str(digit)
self.curr += digit_str
self.curr = self._format(self.curr)
self.screen.set_title(self.curr)

Expand Down
25 changes: 14 additions & 11 deletions pygame_menu/examples/other/maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class AStarQueue:
A* Queue.
"""

def __init__(self):
def __init__(self) -> None:
self.myheap = []

def show(self):
return self.myheap

def push(self, priority, distance, node):
def push(self, priority, distance, node) -> None:
heapq.heappush(self.myheap, (priority, distance, node))

def pop(self):
Expand All @@ -67,13 +67,13 @@ class PriorityQueue:
Priority Queue.
"""

def __init__(self):
def __init__(self) -> None:
self.myheap = []

def show(self):
return self.myheap

def push(self, priority, node):
def push(self, priority, node) -> None:
heapq.heappush(self.myheap, (priority, node))

def pop(self):
Expand All @@ -87,14 +87,14 @@ class PrioritySet:
Create a priority queue that doesn't add duplicate nodes.
"""

def __init__(self):
def __init__(self) -> None:
self.myheap = []
self.myset = set()

def show(self):
return self.myheap

def push(self, priority, node):
def push(self, priority, node) -> None:
if node not in self.myset:
heapq.heappush(self.myheap, (priority, node))
self.myset.add(node)
Expand Down Expand Up @@ -1074,7 +1074,7 @@ def _clear_visited(self) -> None:
self._grid[row][column].update(is_visited=False, is_path=False)
self._update_gui()

def _update_path(self) -> bool | _MazeType:
def _update_path(self) -> bool:
"""
Updates the path.
"""
Expand Down Expand Up @@ -1403,11 +1403,14 @@ def _xfs(
# Trace back to start using path_dict
path_node = goal_node
while True:
path_node = path_dict[path_node]
mazearray[path_node[0]][path_node[1]].update(is_path=True) # type: ignore
self._draw_square(mazearray, path_node[0], path_node[1]) # type: ignore
parent = path_dict[path_node]
if parent is None:
break
path_node = parent
mazearray[path_node[0]][path_node[1]].update(is_path=True)
self._draw_square(mazearray, path_node[0], path_node[1])
if self._visualize:
self._update_square(path_node[0], path_node[1]) # type: ignore
self._update_square(path_node[0], path_node[1])
if path_node == start_point:
return True

Expand Down
15 changes: 6 additions & 9 deletions pygame_menu/widgets/widget/colorinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ def __init__(
), 'invalid hex format mode, it must be "none", "lower" or "upper"'

maxchar: int = 0
valid_chars: list[str] | None = None
self._color_type = color_type.lower()
if self._color_type == COLORINPUT_TYPE_RGB:
maxchar = 11 # RRR,GGG,BBB
self._valid_chars = [
valid_chars = [
"0",
"1",
"2",
Expand All @@ -190,7 +191,7 @@ def __init__(
]
elif self._color_type == COLORINPUT_TYPE_HEX:
maxchar = 7 # #XXYYZZ
self._valid_chars = [
valid_chars = [
"a",
"A",
"b",
Expand Down Expand Up @@ -218,6 +219,8 @@ def __init__(

# noinspection PyArgumentEqualDefault
super().__init__(
alt_x_enabled=False,
apply_widget_update_callback=False,
copy_paste_enable=False,
cursor_color=cursor_color,
cursor_switch_ms=cursor_switch_ms,
Expand All @@ -238,7 +241,7 @@ def __init__(
text_ellipsis="",
textinput_id=colorinput_id,
title=title,
valid_chars=self._valid_chars,
valid_chars=valid_chars,
*args,
**kwargs,
)
Expand All @@ -257,12 +260,6 @@ def __init__(
self._prev_width_factor = prev_width_factor
self._previsualization_surface = None

# Disable parent callbacks
self._apply_widget_update_callback = False

# Disable alt+x
self._alt_x_enabled = False

def _apply_font(self) -> None:
super()._apply_font()

Expand Down
2 changes: 1 addition & 1 deletion pygame_menu/widgets/widget/dropselect_multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def __init__(
selection_option_selected_bgcolor=selection_option_selected_bgcolor,
selection_option_selected_font_color=selection_option_selected_font_color,
title=title,
args=args,
**kwargs,
)

Expand All @@ -261,7 +262,6 @@ def __init__(
assert isinstance(max_selected, int) and max_selected >= 0

# Configure parent
self._args = args or []
self._max_selected = max_selected
self._selection_option_left_space_margin = selection_option_selected_box_margin

Expand Down
8 changes: 4 additions & 4 deletions pygame_menu/widgets/widget/textinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ class TextInput(Widget):
"""

_absolute_origin: Tuple2IntType
_alt_x_enabled: bool
_apply_widget_update_callback: bool # Used in ColorInput
_block_copy_paste: bool
_clock: pygame.time.Clock
_copy_paste_enabled: bool
Expand Down Expand Up @@ -228,6 +226,8 @@ def __init__(
self,
title: Any,
textinput_id: str = "",
apply_widget_update_callback: bool = True,
alt_x_enabled: bool = True,
copy_paste_enable: bool = True,
cursor_color: ColorInputType = (0, 0, 0),
cursor_selection_color: ColorInputType = (30, 30, 30, 100),
Expand Down Expand Up @@ -406,10 +406,10 @@ def __init__(
self._valid_chars = valid_chars

# Callbacks
self._apply_widget_update_callback = True
self._apply_widget_update_callback = apply_widget_update_callback

# Other
self._alt_x_enabled = True
self._alt_x_enabled = alt_x_enabled
self._copy_paste_enabled = copy_paste_enable
self._current_underline_string = ""
self._input_type = input_type
Expand Down
21 changes: 12 additions & 9 deletions test/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
import pygame
import pytest

import pygame_menu
from pygame_menu.baseimage import IMAGE_EXAMPLE_PYGAME_MENU, BaseImage
from pygame_menu.font import FONT_8BIT
from pygame_menu.widgets.widget.button import Button
from pygame_menu.widgets.widget.none import NoneWidget
from test._utils import TEST_THEME, MenuUtils, surface

TEST_TIME_DRAW = False


@pytest.mark.skipif(not TEST_TIME_DRAW, reason="Timing test disabled")
def test_time_draw():
widg = pygame_menu.widgets.NoneWidget()
widg = NoneWidget()
deco = widg.get_decorator()
deco.cache = True

Expand All @@ -37,7 +40,7 @@ def test_time_draw():


def test_cache():
widg = pygame_menu.widgets.NoneWidget()
widg = NoneWidget()
deco = widg.get_decorator()
deco.cache = True

Expand Down Expand Up @@ -87,7 +90,7 @@ def test_cache():


def test_copy():
widg = pygame_menu.widgets.NoneWidget()
widg = NoneWidget()
deco = widg.get_decorator()

with pytest.raises(Exception):
Expand All @@ -97,7 +100,7 @@ def test_copy():


def test_add_remove():
widg = pygame_menu.widgets.NoneWidget()
widg = NoneWidget()
deco = widg.get_decorator()

d = deco._add_none()
Expand Down Expand Up @@ -133,7 +136,7 @@ def test_enable_disable():
def fun(surf, obj):
test[0] = True
assert isinstance(surf, pygame.Surface)
assert isinstance(obj, pygame_menu.widgets.Button)
assert isinstance(obj, Button)

call_id = deco.add_callable(fun)
assert not test[0]
Expand Down Expand Up @@ -197,7 +200,7 @@ def test_general():
deco.add_circle(1, 1, 100, color, False, 5)
deco.add_circle(50, 50, 100, color, True)

img = pygame_menu.BaseImage(pygame_menu.baseimage.IMAGE_EXAMPLE_PYGAME_MENU)
img = BaseImage(IMAGE_EXAMPLE_PYGAME_MENU)
img.scale(0.15, 0.15)
deco.add_surface(60, 60, img.get_surface(), prev=False)

Expand Down Expand Up @@ -244,9 +247,9 @@ def test_general():
assert deco._coord_cache[img_dec] == (300, 173, ((300, 173),))

with pytest.raises(ValueError):
deco.add_text(100, 200, "nice", pygame_menu.font.FONT_8BIT, 0, color)
deco.add_text(100, 200, "nice", FONT_8BIT, 0, color)

deco.add_text(-150, 0, "nice", pygame_menu.font.FONT_8BIT, 20, color, centered=True)
deco.add_text(-150, 0, "nice", FONT_8BIT, 20, color, centered=True)
menu.draw(surface)

with pytest.raises(AssertionError):
Expand Down
2 changes: 0 additions & 2 deletions test/test_sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

import copy
import time
from pathlib import Path

import pygame
import pytest

import pygame_menu
from pygame_menu.sound import (
SOUND_EXAMPLES,
SOUND_INITIALIZED,
Expand Down
4 changes: 2 additions & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import pytest

import pygame_menu
import pygame_menu.utils as ut
from pygame_menu.locals import POSITION_NORTHWEST
from pygame_menu.widgets.widget.button import Button


def test_alpha():
Expand Down Expand Up @@ -46,7 +46,7 @@ def test_padding(value, expected):


def test_terminal_widget_title():
w = pygame_menu.widgets.Button("epic")
w = Button("epic")
w.hide()
s = ut.widget_terminal_title(w)
assert "╳" in s
Expand Down
Loading