Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When UIButton is disabled, its text will never change. #633

Open
Jason2013 opened this issue Sep 7, 2024 · 1 comment
Open

When UIButton is disabled, its text will never change. #633

Jason2013 opened this issue Sep 7, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Jason2013
Copy link

Describe the bug
A clear and concise description of what the bug is.

When UIButton is disable, it cannot responds to click event. This is expected behavior. However, it should change its text even it is disabled.

To Reproduce
Steps to reproduce the behaviour:

Sample code:

import pygame
import pygame_gui

# Initialize pygame
pygame.init()

# Set up the display
window_size = (800, 600)
window_surface = pygame.display.set_mode(window_size)
pygame.display.set_caption('Button Click Example')

# Set up the GUI manager
manager = pygame_gui.UIManager(window_size)

# Create buttons
button1 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((300, 275), (200, 50)),
                                       text='0',
                                       manager=manager)
button2 = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((300, 350), (200, 50)),
                                       text='Toggle Button1',
                                       manager=manager)

# Variables to track button1 state
button1_click_count = 0
button1_enabled = True

# Main loop
clock = pygame.time.Clock()
is_running = True

while is_running:
    time_delta = clock.tick(60) / 1000.0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False

        if event.type == pygame.USEREVENT:
            if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
                if event.ui_element == button1:
                    button1_click_count += 1
                    button1.set_text(str(button1_click_count))
                elif event.ui_element == button2:
                    button1_enabled = not button1_enabled
                    button1.disable() if not button1_enabled else button1.enable() # !!! BUG, the disabled button1 never changed its disabled text caption

        manager.process_events(event)

    manager.update(time_delta)
    window_surface.fill((0, 0, 0))
    manager.draw_ui(window_surface)

    pygame.display.update()

pygame.quit()

  1. When I click button1, its number will increase by 1.
  2. When I click button2 to disable button1, the button always keep the number when the button1 is first disabled. I hope when the button1 is enabled and disabled multiple times, button1's disabled text is the last text when button1 is enabled.

Expected behaviour
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Platform and software (please complete the following information):

  • OS: [e.g. Windows 10]
  • Pygame GUI version [0.6.9]
  • Pygame version [2.6.0]
(venv) C:\chenchang\study\pygame_study>systeminfo | findstr /B /C:"OS Version"
OS Version:                10.0.19045 N/A Build 19045

(venv) C:\chenchang\study\pygame_study>python -V
Python 3.10.6

(venv) C:\chenchang\study\pygame_study>python -m pip list
Package     Version
----------- -------
pip         24.2
pygame      2.6.0
pygame-gui  0.6.9
python-i18n 0.3.9
setuptools  63.2.0

Additional context
Add any other context about the problem here.

I change the following code, and the behavior is my expected:

venv\Lib\site-packages\pygame_gui\core\drawable_shapes\drawable_shape.py
class DrawableShapeState:
    """
    Represents a single state of a drawable shape.

    :param state_id: The ID/name of this state.

    """
    def __init__(self, state_id: str):

        self.state_id = state_id
        self.surface = pygame.surface.Surface((0, 0), flags=pygame.SRCALPHA, depth=32)
        self.has_fresh_surface = False
        self.cached_background_id = None  # type: Union[str, None]
        self.transition = None  # type: Union[DrawableStateTransition, None]

        #self.should_auto_pregen = True #self.state_id != 'disabled'  # !!! THIS IS MY CHANGE: assign value is always True
        self.should_auto_pregen = self.state_id != 'disabled'
        self.generated = False


But I am not sure my change is appropriate for the whole library. Please verify the bug and fix it.

button_disable
button_disable2

@Jason2013 Jason2013 added the bug Something isn't working label Sep 7, 2024
@Jason2013
Copy link
Author

When I upgrade to pygame_gui 0.6.12, the bug is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant