0

I'm making something similar to the pygame Python module.

I'm trying to make something simple and easy to use and making it as practical as possible. I use tkinter for basically everything except sound.

I'm having an issue where I'm loading an image on a tkinter label but it turns out with a black background.

Like this:

An image showing the effect I'm describing

Here is my code for rendering images:

def draw_sprite(self, path: str, x: int, y: int, width: int, height: int):
        """
        Draw an image at a the specified `x` and `y` cordinates with a `width` and `height`.
        """
        try:
            img = Image.open(path)
            img = img.resize((width, height), Image.ANTIALIAS)

            img = ImageTk.PhotoImage(img)

            panel = Label(self.window, image = img)
            panel.image = img
            panel.place(x=x, y=y, width=width, height=height)

            return panel
        except TclError:
            pass

Any help would be appreciated!

EDIT:

I eventually found a solution so this questionis closed now, thanks for anyoen who tried helping.

You can find the module at https://pypi.org/project/realityengine/

Spooky
  • 81
  • 1
  • 6
  • what image do you use - jpg, png ? `Label` can't have transparent background. If you want to use image with transparent backgroung then you should use only `Canvas` – furas Oct 02 '22 at 20:35
  • Hi, I tried with a canvas, it doesn't actually load the image anymore.. – Spooky Oct 02 '22 at 20:39
  • if you get error then show it in question. OR maybe you have standard problem with bug in `PhotoImage` and you have to assign it to global variable - similar to your `panel.image = img` – furas Oct 02 '22 at 20:43
  • using `except TclError: pass` is not good idea - you may have some error but you can't see it. You could at least use `except TclError as ex: print("ex:", ex)` – furas Oct 02 '22 at 20:45
  • I have no idea what you have in code - so I have no idea what is your problem with `parent`. Usually parent means widgets in which you put element. But if you put image on canvas then you should use `canvas.create_image()` and this doesn't need `parent` – furas Oct 02 '22 at 20:55
  • When I run my code with a canvas it gives me the error ```python ex: 'PhotoImage' object has no attribute 'read' ``` – Spooky Oct 02 '22 at 21:08
  • maybe run code with try/except to see full error and show it in question. – furas Oct 02 '22 at 21:20
  • the error is coming from PIL – Spooky Oct 02 '22 at 21:29
  • always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. – furas Oct 03 '22 at 06:59
  • Your function works for me without problem. I never met this type of error - maybe you have problem in different place - with different code. Maybe `panel.image` was used in some other place as file – furas Oct 03 '22 at 07:07
  • Here is the full error: ```python Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\*****\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 2979, in open fp.seek(0) AttributeError: 'PhotoImage' object has no attribute 'seek' ``` – Spooky Oct 03 '22 at 20:32
  • @furas What would that have to do with the white background? – Spooky Oct 03 '22 at 20:35
  • you would have to show line which makes this problem. it seems you mix `PIL.Image` with `PhotoImage` in wrong way. `Image.open()` needs `filename` or `file-like object` but you could put `PhotoImage` - and this can make problem. Maybe use `print(...)` `print(type(...))` to check what you have in variables in line which makes problem. – furas Oct 03 '22 at 20:48

0 Answers0