{"id":8641,"date":"2021-02-19T07:03:12","date_gmt":"2021-02-19T06:03:12","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=8641"},"modified":"2021-02-19T07:03:13","modified_gmt":"2021-02-19T06:03:13","slug":"animate-gif-in-tkinter","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/animate-gif-in-tkinter\/","title":{"rendered":"Animate gif in tkinter"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-39.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"402\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-39-960x402.png\" alt=\"\" class=\"wp-image-8642\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-39-960x402.png 960w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-39-320x134.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-39-768x322.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-39.png 1253w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><figcaption>Animated gif in tkinter<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to see an animated gif in tkinter use this script<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import tkinter as tk\nfrom PIL import Image, ImageTk\nfrom itertools import count, cycle\n\nclass ImageLabel(tk.Label):\n    \"\"\"\n    A Label that displays images, and plays them if they are gifs\n    :im: A PIL Image instance or a string filename\n    \"\"\"\n    def load(self, im):\n        if isinstance(im, str):\n            im = Image.open(im)\n        frames = []\n\n        try:\n            for i in count(1):\n                frames.append(ImageTk.PhotoImage(im.copy()))\n                im.seek(i)\n        except EOFError:\n            pass\n        self.frames = cycle(frames)\n\n        try:\n            self.delay = im.info['duration']\n        except:\n            self.delay = 100\n\n        if len(frames) == 1:\n            self.config(image=next(self.frames))\n        else:\n            self.next_frame()\n\n    def unload(self):\n        self.config(image=None)\n        self.frames = None\n\n    def next_frame(self):\n        if self.frames:\n            self.config(image=next(self.frames))\n            self.after(self.delay, self.next_frame)\n\n#demo :\nroot = tk.Tk()\nlbl = ImageLabel(root)\nlbl.pack()\nlbl.load('tenor.gif')\nroot.mainloop()\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">the output is visible in this repl<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/repl.it\/@EducationalChan\/WastefulSoupyFactors\">https:\/\/repl.it\/@EducationalChan\/WastefulSoupyFactors<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">and in this video too<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"640\" style=\"aspect-ratio: 640 \/ 640;\" width=\"640\" controls src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/tk-2021-02-19-06-53-36.mp4\"><\/video><figcaption>The gif in tkinter<\/figcaption><\/figure>\n\n\n","protected":false},"excerpt":{"rendered":"If you want to see an animated gif in tkinter use this script the output is visible in this repl https:\/\/repl.it\/@EducationalChan\/WastefulSoupyFactors and in \n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/animate-gif-in-tkinter\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":8644,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[527,478,49,923],"tags":[219,87,51],"class_list":["post-8641","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-animation","category-gif","category-tkinter","category-tkinter-advanced","tag-animation","tag-gif","tag-tkinter"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/8641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=8641"}],"version-history":[{"count":1,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/8641\/revisions"}],"predecessor-version":[{"id":8645,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/8641\/revisions\/8645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/8644"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=8641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=8641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=8641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}