0

I am trying to have an image show up on a tkinter window. I have managed to do so in the past, but somehow my current attempt is failing at every step. Hopefully someone can guide me to the proper way and help me fix it.

I'm currently trying with this code. The error I'm getting is

_tkinter.TclError: image "paco_img" doesn't exist

from tkinter import *



PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
BLUE = "#678ac2"
FONT_NAME = "Courier"


window = Tk()
window.title("Thomas' Elevator Pitch")
window.config(padx=200, pady=100, bg=BLUE)

canvas = Canvas(width=5000, height=4000)

paco_img = PhotoImage(file="paco.png")
canvas.create_image(2500, 2000, image="paco_img")
canvas.pack()


I've also tried to do the following, which changes the error to

NameError: name 'ImageTk' is not defined. Did you mean: 'Image'?

However, when I do change ImageTk to Image, it shows PhotoImage as an unresolved attribute reference to Image.



window = Tk()
window.title("Thomas' Elevator Pitch")
window.config(padx=200, pady=100, bg=BLUE)

canvas = Canvas(width=5000, height=4000)

paco_img = ImageTk.PhotoImage(file="paco.png")
canvas.create_image(2500, 2000, image="paco_img")
canvas.pack()


I can't seem to wrap my head around it, and suggestions on similar questions asked here didn't work for me yet.

4
  • 1
    For the first error _tkinter.TclError: image "paco_img" doesn't exist, it's likely because paco.png needs to be the/full/path/to/paco.png instead. For the second error NameError: name 'ImageTk' is not defined. Did you mean: 'Image'?, you'll need to import ImageTk in order to use it: from PIL import Image, ImageTk - ImageTk is part of PIL, not tkinter Commented Nov 22, 2022 at 16:16
  • image=paco_img without quotes Commented Nov 22, 2022 at 16:39
  • Canvas image objects anzeljg.github.io/rin2/book2/2405/docs/tkinter/… Commented Nov 22, 2022 at 16:44
  • possible without Pillow python-course.eu/tkinter/… Commented Nov 22, 2022 at 16:50

1 Answer 1

0

As very well explained in comments by @JRiggles, first you need a Python library to handle the image.

Of course, you can install it with pip:

pip install Pillow

Then, using the tkinter hello-world-program as base, all you need to do is load the necessary libraries, load your image and to display it use either a button or a label widget image param:

from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk



root = Tk()
image = im = Image.open("/path/to/your/image.ext")
frm = ttk.Frame(root, padding=10)
frm.grid()
tk_image = ImageTk.PhotoImage(image)
ttk.Label(frm, image=tk_image).grid(column=0, row=0)
root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.