Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. from tkinter import *
  2. from PIL import Image, ImageTk
  3. from itertools import count, cycle
  4. canvas= Canvas(width=300,height=400,bg='white')
  5. canvas.pack(expand=YES, fill=BOTH)
  6. class ImageLabel(Label):
  7.  
  8. def load(self, im):
  9. if isinstance(im, str):
  10. im = Image.open(im)
  11. frames = []
  12.  
  13. try:
  14. for i in count(1):
  15. frames.append(ImageTk.PhotoImage(im.copy()))
  16. im.seek(i)
  17. except EOFError:
  18. pass
  19. self.frames = cycle(frames)
  20.  
  21. try:
  22. self.delay = im.info['duration']
  23. except:
  24. self.delay = 100
  25.  
  26. if len(frames) == 1:
  27. self.config(image=next(self.frames))
  28. else:
  29. self.next_frame()
  30.  
  31. def unload(self):
  32. self.config(image=None)
  33. self.frames = None
  34.  
  35. def next_frame(self):
  36. if self.frames:
  37. self.config(image=next(self.frames))
  38. self.after(self.delay, self.next_frame)
  39.  
  40. root = Toplevel()
  41. lbl = ImageLabel(root)
  42. lbl.load('images/k.gif')
  43. lbl.grid(row=0)
  44. lbl1 = ImageLabel(root)
  45. lbl1.load('images/k.gif')
  46. lbl1.grid(row=0,column=1)
  47. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement