0

I am trying to create a weather App in Tkinter GUI. It is working fine. I want to add a background image to it but it wont show up. initially i did it without the reference line but it didnt work. On some website they said to keep reference of the image but that too didnt work. Also my Tkinter window size is 1920x1080 and the image was of same dimension but it still doesnt show up. I tried to reduce image size than window but still not working. Any suggestions accepted. Also there is no error.

bg = PhotoImage('clearsky2.jpg')
bgl = Label(gui,image=bg)
bgl.image = bg #given a reference
bgl.place(x=0, y=0, relwidth=1,relheight=1)
bgl.pack()
  • Although you create a reference, you pass the `bg` instead of it as the Label's image argument, and, consequently, it has no effect. You need to give the reference before you assign an image to the `bgl` label, and **pass this reference, not the bg** to the `bgl` Label. – Demian Wolf Aug 29 '20 at 14:43
  • 1
    Also, AFAIK `tkinter.PhotoImage` doesn't support JPEG images, although `PIL.ImageTk` does. – Demian Wolf Aug 29 '20 at 14:44

1 Answers1

7

Its sad that tkinter.PhotoImage does not support JPEG files, but it does support PNG in the newer version and has proper support for GIF too. To use JPEG you need PIL installed. In the terminal say:

pip install Pillow

After that import it like:

from PIL import Image,ImageTk

Then now, to open the image with PIL, say:

img_file = Image.open('clearsky2.jpg')
bg = ImageTk.PhotoImage(img_file)
bgl = Label(gui,image=bg)
bgl.place(x=0, y=0, relwidth=1,relheight=1)

This will work with JPEG and PNG files as well and, keep in mind you dont need to keep a reference unless your looping over images or creating image inside of a function.

Hope it cleared your problem, do let me know if any more errors

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • Hey I am having errors. I tried installing PIL in anaconda prompt but it said "cant find a version that satisfies requirement PIL". And there is already installed PIL in anaconda. I tried your code for using it but it has another error "module 'PIL.Image' has no attribute 'Open' " What must I do? Also I tried this https://stackoverflow.com/questions/10748822/img-image-openfp-attributeerror-class-image-has-no-attribute-open –  Aug 30 '20 at 10:27
  • @Coder123 `PIL.Image`? try from `PIL import Image` – Delrius Euphoria Aug 30 '20 at 10:42
  • Yes I did from PIL import Image, ImageTk –  Aug 30 '20 at 13:21
  • which version of python are you using, and what is the error – Delrius Euphoria Aug 30 '20 at 13:24
  • I am using python 3.7 and it gives error " PIL.Image' has no attribute 'Open' " though I did PIL import Image, ImageTk –  Sep 01 '20 at 05:06
  • @Coder123 try `import PIL` and say `PIL.Image.Open(...)` – Delrius Euphoria Sep 01 '20 at 05:08
  • Hey Thanks it worked! But it is not showing on entire window this is the output https://drive.google.com/file/d/13SZQK9i1uXd7Qq4p-3KMTMZnhSFJJtXM/view?usp=sharing –  Sep 01 '20 at 05:19
  • Yes whole window. But I want it above labels. I mean i want only text and those entry panes and button above image. Rest must be image and not the grey window –  Sep 01 '20 at 05:23
  • Actually that depends on the labels background and so on. You might need to ask a new Q for that – Delrius Euphoria Sep 01 '20 at 05:26
  • 1
    Yes I just now tried changing width and background showed up on more area. I will ask new question. Thank you for your help! It worked! –  Sep 01 '20 at 05:28
  • @Coder123 hi, did `PIL.Image.Open(...)` work? actually i think it was `from PIL import Image` and then `Image.open(...)` with small 'o'. – Delrius Euphoria Sep 02 '20 at 18:37
  • 1
    Yes it worked ...I know that o in open is small I tried to edit your answer but they said minimum 6 characters edit needed..so I ignored it and I did from PIL import Image, ImageTk –  Sep 03 '20 at 19:07