0

Goal: make "tomato.png"(the image on the left) the background of the window on the right.

The window wouldn't even come up when I'd run the code & I'd get this message:

_tkinter.TclError: couldn't recognize data in image file "tomato.png"

In previous work with tkinter I used tkk. to make words show up on a button I was using, I tried it here to no avail(This method was me hip-firing. I've only been studying Python since Dec, 31- there's a lot I do not know.)

I googled my issue and have a cursory knowledge of Pil/Pillow...so I installed Pillow through Pycharm.

There was some progress made. After installing Pillow through Pycharm the window actually came up, the error message did not, but...the image did not produce.

Also, this is the path to the python I am using: /Users/my_name/.pyenv/versions/3.10.0/bin/python

I would appreciate all the help you can give and keep in mind...I'm just two weeks into python with a background in finance- I am not pally with programming parlance...at all. Even when researching this issue there were suggestions to install pip through the command line and to be frank, I had no idea what was going on.

Also, this is my first post so don't crucify me if I didn't follow SOP in this post. Some of you guys are ruthless when answering questions.

I appreciate your time.

https://i.stack.imgur.com/BCehz.png the link to the photo of my code should be here^^

But I manually entered it here:

from tkinter import 

from tkinter import ttk

from PIL import Image

from PIL import ImageTk

# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20

# ---------------------------- TIMER RESET ------------------------------- # 

# ---------------------------- TIMER MECHANISM ------------------------------- # 

# ---------------------------- COUNTDOWN MECHANISM ------------------------------- # 

# ---------------------------- UI SETUP ------------------------------- #

window = Tk()
window.title("Pomodoro")

canvas = Canvas(width=200, height=224)
tomato_pic = Image.PhotoImage(file="tomato.png")
canvas.create_image(100, 112, image=tomato_pic)
canvas.pack()

window.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
Fitzgerald
  • 61
  • 7
  • We can't see the link with the photo of your code. However, it is much more convenient if you paste the code in your question as text and surround it with ``` my code ``` which will give it some syntax highlighting. – lane Jan 18 '22 at 04:29
  • That was a really cook trick. Thanks @lane – Fitzgerald Jan 18 '22 at 04:45
  • `Image.PhotoImage(file="tomato.png")` should be `ImageTk.PhotoImage(file="tomato.png")` – acw1668 Jan 18 '22 at 04:59
  • Cannot reproduce the issue after fixing the typo and using my PNG image. – acw1668 Jan 18 '22 at 05:11
  • I tried that and it doesn't produce the image but I appreciate the suggestion! – Fitzgerald Jan 18 '22 at 05:18

3 Answers3

1

I found an alternative here:

https://stackoverflow.com/a/46624625/8146119


    import Tkinter as tk
    from PIL import ImageTk, Image
    window = tk.Tk()
    window.title("tkinter stuff")
    window.geometry("300x300")
    window.configure(background='grey')
    path = "hs.gif"
    img = ImageTk.PhotoImage(Image.open(path))
    panel = tk.Label(window, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    window.mainloop()

I'll have to find out what makes this code work and what makes my code fail. ## UPDATE ## The issue was that I wasn't using the appropriate tkinter, I was using 8.5 when I should have been using 8.6. I was also using python 3.10 and since tkinter, based on my cursory searches online, doesn't work on 3.9. I am using 3.8. The issue I was having is no longer. Hopefully someone will benefit from this. I appreciate everyone's time.

Fitzgerald
  • 61
  • 7
1

I found this post because I had the same problem. I am a two month beginner in python. I have changed the photo format to jpeg and now it works nice, but it losts transparent background... I used it in similar code like the previous one:

from tkinter import *
from PIL import ImageTk

window = Tk()
window.title("Pomodoro")

canvas = Canvas(width=200, height=224)
tomato_pic = Image.PhotoImage(file="tomato.jpeg")# notice .jpeg instead of .png format
canvas.create_image(100, 112, image=tomato_pic)
canvas.pack()

window.mainloop()

I hope it helps. I've tried with different .png files and sometimes works and others not... I'm really happy to have found this site, many thanks everyone!

tgallei
  • 827
  • 3
  • 13
  • 22
0

First, you may have better luck with newer versions of Python. Various stackoverflow posters have noted issues with tkinter supporting images. See post here

According to this stackoverflow answer the tkinter.Label and Label.place are handy tricks for placing background images in tkinter windows. Below is how this may fit into your code. It works for me using Python 3.8 and Windows 10.

from tkinter import *
from PIL import Image
from PIL import ImageTk

# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20

# ---------------------------- TIMER RESET ------------------------------- # 

# ---------------------------- TIMER MECHANISM ------------------------------- # 

# ---------------------------- COUNTDOWN MECHANISM ------------------------------- # 

# ---------------------------- UI SETUP ------------------------------- #

window = Tk()
window.title("Pomodoro")

canvas = Canvas(width=200, height=224)
tomato_pic = PhotoImage(file="tomato.png")
# note here use of Label
background_label = Label(window, image=tomato_pic)
# label.place is tk trick that is good for backgrounds
background_label.place(x=0, y=0, relwidth=1, relheight=1)
canvas.pack()

window.mainloop()

Then you may need to play with the relwidth, relheight parameters and possibly shrink the size of your original image depending to get it to fit properly.

lane
  • 766
  • 5
  • 20
  • Thanks! this is what I got `Traceback (most recent call last): File "/Users/*/Documents/#100daysofCode/pomodoro-start/main.py", line 28, in tomato_pic = PhotoImage(file="tomato.png") File "/Users/*/.pyenv/versions/3.10.0/lib/python3.10/tkinter/__init__.py", line 4093, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "/Users/*/.pyenv/versions/3.10.0/lib/python3.10/tkinter/__init__.py", line 4038, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't recognize data in image file "tomato.png"` – Fitzgerald Jan 18 '22 at 05:17
  • It’s possible there is an issue with your png file. Try to find a true png from the web. Like https://upload.wikimedia.org/wikipedia/commons/9/9d/Tomato.png – lane Jan 18 '22 at 05:19
  • I just tried that...and it isn't working either. I do appreciate your time. I'll keep at it. Thank you very much. – Fitzgerald Jan 18 '22 at 05:28
  • Ok one more update to my answer. You may need to update Python. See the link to another post with the same error – lane Jan 18 '22 at 05:49
  • I have tried specifying the full path before using Label and .place(), I tried it now and got the same "couldn't recognize data" error. I'm using python3.10(via homebrew) in my pycharm interpreter and tkinter 8.5.9. – Fitzgerald Jan 18 '22 at 05:56
  • I found the solution here(https://stackoverflow.com/a/46624625/8146119). I'll have to google what is the significant difference that makes this code work but the code I initially posted doesn't. I appreciate your time @lane!!!! – Fitzgerald Jan 18 '22 at 11:06