Skip to content

Creating AnimatedGif widget

JellyfisHawthorn edited this page Jun 14, 2025 · 1 revision

After importing the module, create the AnimatedGif widget in the module as you would any other widget (instantiate the AnimatedGif class)

AnimatedGif class inherits from tkinter.Frame

TkGifWidget.AnimatedGif(file_path=None, play_mode=CLICK, default_bg=None, bg_func=None, nogif_bg=None,
hover_func=None, play_end_func=None, loop=-1, master=None, **kwargs)
  • file_path: The path to the GIF file.
  • play_mode: There are three play modes:
    • click: Show background image before clicking, play GIF after clicking, click again to redisplay background image.
    • display: Starts playing the GIF when the GIF widget is mapped and ends playing the GIF when it is unmapped.
    • hover: When the mouse moves over the GIF widget, the GIF is played, and the GIF is stopped when the mouse moves out of the GIF widget.
  • default_bg: default background image, the value can be a path or an Image object. The image will be displayed when the GIF is not playing, and the default is the first frame of the GIF.
  • loop: cycle count of GIF. 0 means infinite loop, None means no loop, -1 (default) takes the GIF setting.

total play times = 1 + loop

  • master: Master widget.
  • **kwargs: keyword arguments passed to the Frame widget.

Example:

csae_1.py:

from tkinter import *
from TkGifWidget import *
root = Tk()

# Trigger when clicked
gif1 = AnimatedGif('../gif/example.gif', play_mode='click')
gif1.pack()
# Trigger when hovered, and change background
gif2 = AnimatedGif('../gif/example.gif', play_mode=HOVER, default_bg='example_bg.png')
gif2.pack()
# Play automatically, stop after 2 loops
gif3 = AnimatedGif('../gif/example.gif', play_mode=DISPLAY, loop=1)
gif3.pack()
# Add a border by modifying the Frame widget
gif4 = AnimatedGif('../gif/example.gif', DISPLAY, relief=SOLID, border=5)
gif4.pack()

root.mainloop()

Result:

case_1.gif

Clone this wiki locally