1

I am trying to get an image to display in a tkinter canvas.

I know the Canvas works with shapes and text, but image isn't working.

I am using PIL ImageTK.PhotoImage, however it also doesn't work creating a Tkinter.PhotoImage using a '.ppm' image.

The 'png' image is stored in the same directory as the python file.

import tkinter as tk
from PIL import Image, ImageTk


class Window:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("COMP")
        self.window.geometry("1200x600")

        topframe = tk.Frame(self.window, highlightbackground='black', highlightthickness=1)
        topframe.pack(side='top')
        self.noteview = NoteView(topframe, self.songString)


class NoteView:
    def __init__(self, frame):
        self.canvas = tk.Canvas(frame, width=60, height=200)
        self.canvas.pack()

        self.canvas.create_text(15, 190, text='hi')
        im = Image.open('png.png')
        self.image = ImageTk.PhotoImage(im)
        self.canvas.create_image(20, 20, anchor='nw', image=self.image)

w = Window()

TypeError: 'PhotoImage' object is not callable

stovfl
  • 14,998
  • 7
  • 24
  • 51
adam
  • 51
  • 1
  • 6
  • try `self.panel = Label(root, image = im) self.panel.pack(side = "bottom", fill = "both", expand = "yes")` – learner Oct 23 '19 at 05:38
  • @learner unfortunately I need to do it on a canvas, so I can layer other things below it. – adam Oct 23 '19 at 06:01
  • sorry for that. Can you point at which line is giving this error? – learner Oct 23 '19 at 06:33
  • Read [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/a/16424553/7414759) – stovfl Oct 23 '19 at 11:03

1 Answers1

0

The proble is that the tkinter somehow bings the image to the window, not in your case. You should use self.canvas.image = ImageTk.PhotoImage(im) and self.canvas.create_image(20, 20, anchor='nw', image=self.canvas.image). Hope that's helpful!

rizerphe
  • 1,340
  • 1
  • 14
  • 24