1

I'm currently working on a project based on pygame. It is the first time I'm attempting a project like this. I choose to try the pygmae_gui library in order to (who wold have guessed) implement a GUI to the project.

I successfully added some buttons to the application without any problems. I wrote a part where a new surface pops up when a certain button is pressed (which works without issues).

I want to add buttons on this new surface, but i don't know how.

My code looks something like this:

def _set_up_surfaces():
    screen = pygame.display.set_mode((self.width, self.height))
    background = pygame.Surface((self.width, self.height))
    new_layer = pygame.Surface((440, 360))


def _set_up_click_button():
    start_button_rect = pygame.Rect(0, 0, 150, 30)
    bye_button_layout_rect = pygame.Rect(0, 0, 150, 30)
    bye_button_layout_rect.bottomright = (-30, -20)
    close_button_rect = pygame.Rect(320, 320, 150, 30)
    start_button = pygame_gui.elements.UIButton(relative_rect=start_button_rect, text="START",manager=gui_manager)
    new_layer_close_button = pygame_gui.elements.UIButton(relative_rect=close_button_rect, text='close', manager=self.gui_manager, container=new_layer)

active = True
# set up start display:
new_layer_visible = False
set_up_surfaces()
set_up_click_button()
# main game loop
while active:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            active = False
        if event.type == pygame_gui.UI_BUTTON_PRESSED:
            if event.ui_element == self.start_button:
                self.machine_visible = True
            if event.ui_element == self.bye_button:
                print('Good Bye!')
                active = False
        self.gui_manager.process_events(event)

    if new_layer_visible:
        new_layer_visible.blit(imported_drawing, (0, 0))
        self.background.blit(new_layer, (140, 60))
    gui_manager.update(self.time_delta)

    screen.blit(self.background, (0, 0))
    gui_manager.draw_ui(self.screen)

    pygame.display.update()

I know that the code does not run as I wrote it here since I only included the part I thought is relevant.

I tried to set the container argument to the surface as I saw as a suggestion online but I got an Error as response:

ValueError: container parameter must be of type IContainerLikeInterface.

The objective is to put the "close-Button" on the new_layer surface. Is this possible using pygame_gui, should I use a different library?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    You cannot add a button to a surface because a surface consists only of a grid of pixels and cannot contain objects. And anyway Pygame GUI is built on Pygame and not vice versa, so a Pygame object can't handle a Pygame GUI object. In short a `Surface` is not a `IContainerLikeInterface` – Rabbid76 Feb 01 '23 at 18:28

1 Answers1

1

You cannot add a Pygame GUI button to a pygame.Surface object because a pygame.Surface consists only of a grid of pixels and cannot contain objects. Also, the Pygame GUI is built on top of Pygame, not the other way around, so a Pygame object cannot handle a Pygame GUI object and no Pygame object implements the interface IContainerLikeInterface. You have to draw the Surface manually in the background and put the GUI on top of it.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174