ppizarror / pygame-menu

A menu for pygame. Simple, and easy to use
https://pygame-menu.readthedocs.io/
Other
555 stars 141 forks source link

Documentation example produces a RuntimeError #483

Closed luigibrancati closed 4 weeks ago

luigibrancati commented 4 months ago

Environment information Describe your environment information, such as:

Describe the bug Using the code from display-a-menu section gives RuntimeError: menu is not enabled error.

To Reproduce Minimal code is taken from the documentation (with some additional code required by pygame)

import pygame as pg
import pygame_menu as pgm

def disable_menu():
    global menu
    menu.disable()

pg.init()
screen = pg.display.set_mode((1280, 720))
menu = pgm.Menu('Welcome', 1280, 200, position=(0, 100))
menu.add.button('Quit', disable_menu)
running = True
clock = pg.time.Clock()

while running:
    clock.tick(60)
    screen.fill("black")
    events = pg.event.get()
    for event in events:
        if event.type == pg.QUIT:
            running = False
    if menu.is_enabled():
        menu.update(events)
        menu.draw(screen) # Code breaks here
    pg.display.update()

pg.quit()

The code above gives

pygame 2.5.2 (SDL 2.28.2, Python 3.12.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame-menu 4.4.3
Traceback (most recent call last):
  File "/home/luigi/projects/planet-sim/planet_sim/test_menu copy.py", line 26, in <module>
    menu.draw(screen)
  File "/home/luigi/miniconda3/envs/planet-sim/lib/python3.12/site-packages/pygame_menu/menu.py", line 2053, in draw
    self._current._runtime_errors.throw(self._current._runtime_errors.draw, 'menu is not enabled')
  File "/home/luigi/miniconda3/envs/planet-sim/lib/python3.12/site-packages/pygame_menu/menu.py", line 4090, in throw
    raise RuntimeError(msg)
RuntimeError: menu is not enabled

Expected behavior I would expect the documentation example to work.

Switching the draw and update functions solves the problem

...

    if menu.is_enabled():
        menu.draw(screen) # Doesn't break
        menu.update(events)
...

Additional context

ppizarror commented 4 weeks ago

Thanks, I fixed the documentation!