0

Before I start I do know there is a post regarding my question found here, however, this didn't exactly help me and I still seem to be running into problems even after following answers on the post.

I seem to get two different "errors"

One error, where it shows that the image file is unreadable. which is given when using this code(This is what is given from the other post as a answer that worked):

self.background_image=tk.PhotoImage…
self.background_label = tk.Label(parent, image=background_image)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)

And another "Error" Where when using the following code, no image shows up but no real error message is given:

self.background_image = tk.PhotoImage(r'C:/hazuki-gui/resources/background1.png')
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)

I have looked around and everything just shows the first way I showed above. I have tried using both png and jpg images but both return the same results in both cases.

Any help regarding this would be appreciated.

Side Note: If there is any difference for how to do this for python 2.7 and python 3.x, please let me know (Currently I am using python 2.7 but will be moving this to 3.x)

NaruS
  • 168
  • 1
  • 15
  • 1
    `PhotoImage()` does not support PNG or JPG files! GIF is the only common format it supports. Use the PIL/Pillow module to load images of other types. – jasonharper Oct 19 '18 at 13:23
  • I see, I was so confused by this, I also noticed the `Image()` function? I know its not related to my question but what is the use of this seeing as `PhotoImage()` is used for gif files. – NaruS Oct 19 '18 at 13:31
  • @NaruS the current version of python supports `png` i believe. However 2.7 according to the [documentation](http://effbot.org/tkinterbook/photoimage.htm) `PhotoImage class can read GIF and PGM/PPM images`. You should look into PILLOW / PIL if you want to support more file types for images. – Mike - SMT Oct 19 '18 at 13:59
  • did you put `parent` in your 2nd snippet? – Henry Yik Oct 19 '18 at 16:14
  • @HenryYik, I have tried using parent in the 2nd snippet yes, still nothing. – NaruS Oct 20 '18 at 02:04

1 Answers1

1

Maybe your image is garbage collected since there is no reference to your image?

self.background_image = tk.PhotoImage(file=r'C:/hazuki-gui/resources/background1.png')
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image #try to add this
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
  • When adding this, It had no effect. I beleive I may need a different module to do this! – NaruS Oct 20 '18 at 17:39
  • Actually when checking back this was part of my problem, even though it wasn't all I needed to get my code to work, it provided part of it and it was enough to help me out. – NaruS Oct 24 '18 at 16:15