Created
May 7, 2018 03:10
-
-
Save novel-yet-trivial/047cacb5153db0631f068f8ce39180d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
try: | |
import tkinter as tk | |
except ImportError: | |
import Tkinter as tk | |
from PIL import Image, ImageTk | |
from itertools import count, cycle | |
class ImageLabel(tk.Label): | |
""" | |
A Label that displays images, and plays them if they are gifs | |
:im: A PIL Image instance or a string filename | |
""" | |
def load(self, im): | |
if isinstance(im, str): | |
im = Image.open(im) | |
frames = [] | |
try: | |
for i in count(1): | |
frames.append(ImageTk.PhotoImage(im.copy())) | |
im.seek(i) | |
except EOFError: | |
pass | |
self.frames = cycle(frames) | |
try: | |
self.delay = im.info['duration'] | |
except: | |
self.delay = 100 | |
if len(frames) == 1: | |
self.config(image=next(self.frames)) | |
else: | |
self.next_frame() | |
def unload(self): | |
self.config(image=None) | |
self.frames = None | |
def next_frame(self): | |
if self.frames: | |
self.config(image=next(self.frames)) | |
self.after(self.delay, self.next_frame) | |
root = tk.Tk() | |
lbl = ImageLabel(root) | |
lbl.pack() | |
lbl.load('ball-small.gif') | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can i put it also in a tk canvas?