0

I have been creating a file that displays a image on a canvas. I created the 'PhotoImage' file that I use to store my picture.

 d = PhotoImage(file="pic.gif")
 canvas = Canvas(root, 500, 500)
 pic = canvas.create_image(20, 20, image=d)

But I just produce an error each time I run the program...for some reason, PhotoImage will never work for me. How do I get this to work?

Lumater
  • 23
  • 5
  • 1
    this is a very anemic question, provide more detail like what error? (full stack trace would be good) – Nullman Feb 10 '17 at 00:27
  • I couldn't be certain without an [mcve](http://stackoverflow.com/help/mcve) but this might help http://stackoverflow.com/questions/16424091/putting-gif-image-in-a-canvas-with-tkinter – Paul Rooney Feb 10 '17 at 00:29
  • "an error" is useless information. Please post the exact error. – Bryan Oakley Feb 10 '17 at 00:39
  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – ᴇɴᴅᴇʀᴍᴀɴ Sep 19 '22 at 01:20

1 Answers1

0

This is a an example that works for me.

#----------------------------------------------------------------------
import Tkinter
#----------------------------------------------------------------------
root = Tkinter.Tk()
frame = Tkinter.Frame(root)
frame = Tkinter.LabelFrame(root, text="LabelFrame text", padx=5, pady=5)
frame.pack(side=Tkinter.LEFT)
Label1 = Tkinter.Label(frame, text="Label1 text")
Label1.pack()
#----------------------------------------------------------------------
photo1 = Tkinter.PhotoImage(file = 'Chart_Example.gif')
#
width_1 = photo1.width()
height_1 = photo1.height()
#
x_center_1 = width_1 / 2.0
y_center_1 = height_1 / 2.0
#---------------------------------
iframe1 = Tkinter.Frame(frame, bd=2, relief=Tkinter.RAISED)
iframe1.pack(expand=1, fill=Tkinter.X, pady=5, padx=5, side=Tkinter.LEFT)
c1 = Tkinter.Canvas(iframe1, width=width_1, height=height_1)
c1.create_image(x_center_1, y_center_1, image=photo1, anchor = Tkinter.CENTER)
c1.pack(side=Tkinter.LEFT)
#----------------------------------------------------------------------
root.mainloop()
#----------------------------------------------------------------------
R. Wayne
  • 417
  • 1
  • 9
  • 17