user4648074
user4648074

Reputation:

Simplest code to import a .gif

I'm looking for the simplest code to run a .gif in my program. The file is called "vault_ext.gif". So far I have:

from tkinter import *

def vault():
    photo = PhotoImage(file="vault_ext.gif")

vault()

Upvotes: 1

Views: 90

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49318

>>> import tkinter as tk
>>> root = tk.Tk()
>>> w = 500 # replace 500 with the width of your photo
>>> h = 600 # replace 600 with the height of your photo
>>> canvas = tk.Canvas(root, width=w, height=h)
>>> canvas.pack()
>>> img = tk.PhotoImage(file='vault_ext.gif')
>>> img_ref = canvas.create_image(w//2, h//2, image=img)

That is the basic way of getting an image to display. I recommend looking for examples of tkinter applications that use classes so you can more easily implement interactivity. Effbot's tkinterbook is a great resource.

Let's add a binding:

>>> def printer(event):
...     print(event.x, event.y)
...
>>> canvas.bind("<Button-1>", printer)

Every time you left-click on the picture, it prints the x,y coordinates of where you clicked (coordinates start from the top left corner).

Upvotes: 2

Related Questions