ppizarror / pygame-menu

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

Two input text are used simultaneously #443

Closed Merklins closed 1 year ago

Merklins commented 1 year ago

Hello! I want to control each button and I use what you suggested to me, I first render the menu and then I draw, update and set the position of each button. But when I use text inputs, two widgets are used at once. How do I fix this, thanks a lot in advance :)

import sys
import pygame
import pygame_menu

pygame.init()
screen = pygame.display.set_mode((1920, 1080))
clock = pygame.time.Clock()
menus = pygame_menu.Menu('', 1920, 1080, False)

age = menus.add.text_input(
    'Character age:',
)

name = menus.add.text_input(
    'Character name:'
)

menus.render()

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 0, 0))

    age.draw(screen)
    age.update(events)
    age.set_position(500, 500)

    name.draw(screen)
    name.update(events)
    name.set_position(500, 800)

    pygame.display.update()
    clock.tick(60)
ppizarror commented 1 year ago

Hello, if you don't want to use the menu.update(events), you must control which widget receives the events. For such purposes, you can use widget.is_selected(). For example:

import sys
import pygame
import pygame_menu

pygame.init()
screen = pygame.display.set_mode((1920, 1080))
clock = pygame.time.Clock()
menus = pygame_menu.Menu('', 1920, 1080, False)

age = menus.add.text_input(
    'Character age:',
)

name = menus.add.text_input(
    'Character name:'
)

menus.render()
menus._widget_selected_update = False

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 0, 0))
    menus.update(events)
    age.draw(screen)
    if age.is_selected():
        age.update(events)
    age.set_position(500, 500)

    name.draw(screen)
    if name.is_selected():
        name.update(events)
    name.set_position(500, 800)

    pygame.display.update()
    clock.tick(60)

Note that we must avoid Menu passing events to its widget, thus, we set menus._widget_selected_update = False.

ppizarror commented 1 year ago

New version to be released today also adds the possibility to ignore the menu events for a single widget by changing receive_menu_update_events of a widget. For example:

name.receive_menu_update_events = False
menus.update(events)

In such a case name does not receive any input from menus.update.

ppizarror commented 1 year ago

Version updated to v4.3.6. Let me know if this solves your issues,

Merklins commented 1 year ago

Yes, it helped me, thank you very much