MyreMylar / pygame_gui

A GUI system for pygame.
MIT License
678 stars 77 forks source link

Unable to Load Image at Path #77

Closed Snayff closed 4 years ago

Snayff commented 4 years ago

Describe the bug Image not loading in to buttons.

Expected behaviour Expect to see the image in the button. If the path given in the error is pasted into windows explorer it immediately load the image so the path is correct.

Platform and software (please complete the following information):

Additional context Error message:

C:\Users\Gabriel\Documents\NotQuiteParadise\venv\lib\site-packages\pygame_gui\core\ui_appearance_theme.py:248: UserWarning: Unable to load image at path: C:\Users\Gabriel\Documents\NotQuiteParadise\assets\skills\placeholder\icon_09.png
  warnings.warn('Unable to load image at path: ' + str(os.path.abspath(path)))

themes.json:

     "#skill_button4":
     {
         "images":
         {
             "normal_image":
             {
                 "path": "assets/skills/placeholder/icon_08.png"
             },
             "hovered_image":
             {
                 "path": "assets/skills/placeholder/icon_09.png"
             }
         },
MyreMylar commented 4 years ago

Not sure what is causing this, but I have changed this part of the code for 0.5.0 anyway so it may just resolve itself.

If not, things that might help to narrow it down:

  1. Does the image file load in pygame normally with pygame.image.load(your_path_here). I ask because that is most of what goes on in that part of the pygame_gui code.
  2. Do other button images load, i.e. is it: a) just this image. b) any image loaded at the 'hovered_image' stage. c) all images on buttons.
Snayff commented 4 years ago
  1. Yes, the image loads fine if I call it directly.
  2. No button images load, at any stage or with any interaction.
MyreMylar commented 4 years ago

Think this has fairly good test coverage already, guess we'll see if it resolves with the changes made to how paths are calculated before digging in any more.

Snayff commented 4 years ago

That's good to hear - its still an issue, unfortunately. Do you have an ETA for 0.5?

MyreMylar commented 4 years ago

Should be soon, I'm onto working on the documentation now.

Snayff commented 4 years ago

That's fantastic. I am very excited for the new release! Do you think the new version is likely to break a lot of compatibility?

MyreMylar commented 4 years ago

Some. It depends what aspects you are using. The windows have changed a lot because I moved a lot of features I was repeating for each window so they are built in to the core window class.

Most of the regular elements should be pretty much the same interface wise, with some new options.

Snayff commented 4 years ago

Unfortunately, this was not resolved by 0.5.*.

MyreMylar commented 4 years ago

I've solved this in your code. Here:

class _UIManager:
"""
Manage the UI, such as windows, resource bars etc
"""

def __init__(self):
    # first action needs to be to init pygame.
    pygame.init()

    # now init the pygame_gui
    self._gui = UIManager((VisualInfo.BASE_WINDOW_WIDTH,
        VisualInfo.BASE_WINDOW_HEIGHT), "data/ui/themes.json")

    # display info
    # TODO - allow for selection by player but only multiples of base (16:9)
    self._desired_width = VisualInfo.BASE_WINDOW_WIDTH
    self._desired_height = VisualInfo.BASE_WINDOW_HEIGHT
    self._screen_scaling_mod_x = self._desired_width // VisualInfo.BASE_WINDOW_WIDTH
    self._screen_scaling_mod_y = self._desired_height // VisualInfo.BASE_WINDOW_HEIGHT
    self._window: pygame.display = pygame.display.set_mode((self._desired_width, self._desired_height))
    self._main_surface: pygame.Surface = pygame.Surface((VisualInfo.BASE_WINDOW_WIDTH,
        VisualInfo.BASE_WINDOW_HEIGHT), pygame.SRCALPHA)

The UIManager is created before the window, unfortunately pygame requires the window to be created before it can load any images, so you can fix all the loading issues (except the missing icon_00.png) by swapping the order of creation to this:

 class _UIManager:
"""
Manage the UI, such as windows, resource bars etc
"""

def __init__(self):
    # first action needs to be to init pygame.
    pygame.init()

    # display info
    # TODO - allow for selection by player but only multiples of base (16:9)
    self._desired_width = VisualInfo.BASE_WINDOW_WIDTH
    self._desired_height = VisualInfo.BASE_WINDOW_HEIGHT
    self._screen_scaling_mod_x = self._desired_width // VisualInfo.BASE_WINDOW_WIDTH
    self._screen_scaling_mod_y = self._desired_height // VisualInfo.BASE_WINDOW_HEIGHT
    self._window: pygame.display = pygame.display.set_mode((self._desired_width, self._desired_height))
    self._main_surface: pygame.Surface = pygame.Surface((VisualInfo.BASE_WINDOW_WIDTH,
        VisualInfo.BASE_WINDOW_HEIGHT), pygame.SRCALPHA)

    # now init the pygame_gui
    self._gui = UIManager((VisualInfo.BASE_WINDOW_WIDTH,
                           VisualInfo.BASE_WINDOW_HEIGHT), "data/ui/themes.json")
    # elements info
    self._elements = {}  # dict of all init'd ui_manager elements

    # process config
    self._load_display_config()
    self._load_fonts()

    logging.info(f"UIManager initialised.")
Snayff commented 4 years ago

I probably thought I was being smart when I reordered it and didnt notice the loss of those images. You're spot on, moving the init to after the display is set worked. Thank you, mate. I really appreciate your help.