-1

I have a 'Game' class, which has to create a window with image, here's the code that I've tried, but the image does not appear. The directory there exists and no errors are produced

from tkinter import *
class Game():
    def __init__(self):
        self.tk = Tk()
        self.tk.title('          ')
        self.tk.resizable(0, 0)
        self.tk.wm_attributes("-topmost", 1)
        self.canvas = Canvas(self.tk, width=800, height=600)
        self.canvas.pack()
        self.tk.update()
        self.bg = \
        PhotoImage('C:\\Users\\iv4um\\ninjafight\\hj\\officelight.gif')
        self.bgimage = self.canvas.create_image(0, 0, image=self.bg, \
        anchor='nw')
        self.tk.update()

I expect the image to be at the screen, but all I get is blank screen, with no image. Image is 800 * 600 pixels and it's a .gif.

Andrii Chumakov
  • 279
  • 3
  • 12

2 Answers2

-1

Try using the pillow module (PIL, python imaging library), you should use this to handle gifs as images...

from PIL import Image, ImageTk
EcSync
  • 842
  • 1
  • 6
  • 20
-2

If you want to use Tkinter module, you can look Play an animated gif in python with tkinter. Although, the answer uses Label in order to display the gif.

I suggest that you could try pyglet module it is more easy.

import pyglet

# giphy directory
giphy_file = "giphy.gif"

# make an animation object
animation = pyglet.resource.animation(giphy_file)

sprite = pyglet.sprite.Sprite(animation)

# create a window
win = pyglet.window.Window(width=sprite.width, height=sprite.height)

# background color = r, g, b, alpha of the window
green = 0, 1, 0, 1
pyglet.gl.glClearColor(*green)

@win.event
def on_draw():
     win.clear()
     sprite.draw()
pyglet.app.run()
Jacob Fuchs
  • 359
  • 1
  • 3
  • 14