ppizarror / pygame-menu

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

menu.select_widget does not work #445

Closed asztalosdani closed 1 year ago

asztalosdani commented 1 year ago

Environment information Describe your environment information, such as:

Describe the bug I have a submenu, and in the onbeforeopen callback, I want to select a specific item. Calling menu.select_widget() does not do anything, always the first item is selected.

Main menu Submenu
image image

To Reproduce

import pygame_menu
from pygame_menu.examples import create_example_window

def on_before_open(_from, _to):
    _to.select_widget("option2")

surface = create_example_window('Example - Simple', (320, 240))

main_menu = pygame_menu.Menu(height=240, theme=pygame_menu.themes.THEME_BLUE, title='Welcome', width=320)

submenu = pygame_menu.Menu(height=240, theme=pygame_menu.themes.THEME_BLUE, title='Welcome', width=320)
submenu.add.button("Option 1", button_id="option1")
submenu.add.button("Option 2", button_id="option2")
submenu.add.button("Option 3", button_id="option3")
submenu.add.button("Back", pygame_menu.events.BACK)
submenu.set_onbeforeopen(on_before_open)

main_menu.add.button('Submenu', submenu)
main_menu.add.button('Quit', pygame_menu.events.EXIT)

if __name__ == '__main__':
    main_menu.mainloop(surface)

Expected behavior Option 2 should be selected.

image

Additional context I tried passing button_id and the widget instance without luck.

ppizarror commented 1 year ago

I'll review this asap

bluefish2020 commented 1 year ago

I ran into this a bit ago too. _onbeforeopen is called in menu.py line 3190, and after return the first widget is selected. I was planning to reverse those if I couldn't find any other way to make my change stick.

asztalosdani commented 1 year ago

I can confirm, after commenting the self._current._select(0, 1, SELECT_OPEN, False, update_mouse_position=False) my example code works as expected. If I don't call select_widget() in the onbeforeopen callback the first is selected even without the call. Can it be removed completely, or should it be just moved above the callback invocation?

ppizarror commented 1 year ago

Fixed in #448. Let me know if this solves your issue 😄

bluefish2020 commented 1 year ago

That change works perfectly for what I was doing. Thanks!

asztalosdani commented 1 year ago

Yes, it works like a charm! 🎉

ppizarror commented 1 year ago

Version v4.3.7 uploaded to pip!