-1

I was trying to code a small window in a program that is giving you feedback about your statistics in the game. The output is working fine, but the image i tried to edit is not working. I tried to define the image global, i tried to take the direct path, i tried to change the suffix of the picture i want to edit (right now it's a .gif -file) but nothing is working. Whats wrong? What is my mistake?

import tkinter 
def blackhole():    
    Black=tkinter.Tk()
    Black.title("BlackHole")
    schrift=tkinter.Label(Black,text="BlackHole: Game got reseted!")
    schrift.pack()
    Medal=tkinter.Label(Black,text="Congretulation! You earn the Bronze Medal!")
    Medal.pack()
    knopf=tkinter.Button(Black,text="Ok",command=submit)
    knopf.pack()
    canvas = tkinter.Canvas(Black, width=250, height=250)
    canvas.pack()
    tk_img = tkinter.PhotoImage(file = '/Users/Hannes/Desktop/Klickbot/b.gif')
    canvas.create_image(150, 150, image=tk_img)
scrpy
  • 985
  • 6
  • 23
Next TV
  • 31
  • 1
  • 6

1 Answers1

-1

To include images with Tkinter you should use Pillow (PIL).

https://python-pillow.org/

Run pip install Pillow from the terminal to install.

Sample usage:

from PIL import ImageTk, Image
img = ImageTk.PhotoImage(Image.open("logo.png"))
panel = Label(root, image=self.img)
panel.pack(side="top", fill="both", expand="yes")
scrpy
  • 985
  • 6
  • 23
F. Leone
  • 594
  • 3
  • 14
  • The question asks about a .gif image, and with .gif you don't need PIL or Pillow. – Bryan Oakley Dec 07 '17 at 13:05
  • Still the answer is relevant imo because it will work. My bad tho, didn`t pay enough attention on that. Downvoting it is non-sense. – F. Leone Dec 07 '17 at 13:17
  • While it will work, it's still not relevant to the question that was asked. The question was specifically about creating an image in a function, which is a known problem with a known solution. Your answer doesn't address that. I suppose it's possible that Pillow doesn't have the same problems with garbage collection, but since the original problem is actually about garbage collection and you don't address that issue, I don't think this answer is useful because it doesn't explain _why_. – Bryan Oakley Dec 07 '17 at 13:41