pygame-menu icon indicating copy to clipboard operation
pygame-menu copied to clipboard

Documentation example produces a RuntimeError

Open luigibrancati opened this issue 3 weeks ago • 0 comments

Environment information Describe your environment information, such as:

  • OS: Ubuntu 22.04.4 LTS
  • python version: v3.12
  • pygame version: v2.5.2
  • pygame-menu version: v4.4.3

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

luigibrancati avatar Jun 16 '24 17:06 luigibrancati