Skip to content

Modify the GIF, background image and playback mode after creating the widget.

JellyfisHawthorn edited this page Jun 14, 2025 · 1 revision

Modify the GIF

It is possible to instantiate the widget without specifying the file_path parameter, and then set the GIF using the set_gif() method.

AnimatedGif.set_gif(file_path)
  • file_path:GIF file path. If file_path is None, the current GIF animation is specified as empty.

Modify the background image

Use the set_bg_img() method to set the background image.

AnimatedGif.set_bg_img(value)

Set the background image according to the value, and then perform subsequent processing. If value is None, the background image is set to the first frame of the GIF (if there is any); if it is an Image object, it is directly set; otherwise, it is considered as the path of the background image.

Note that this method is used to set the current background image, and if you want to set the default background image, please modify the default_bg attribute.

Modify the playback mode

Use the set_play_mode() method to set the playback mode.

AnimatedGif.set_play_mode(play_mode)

Example:

case_5.py:

from tkinter import *
from tkinter.ttk import Combobox
from TkGifWidget import *
from PIL.Image import new

root = Tk()

# Creating a white background image
bg_img = new('RGB', (400, 100), 'white')
# Create a GIF widget and set the play mode and background image.
# When GIF is not specified, the background image is blank, when GIF is specified, 
# the background image is the first picture of GIF.
gif = AnimatedGif(nogif_bg=bg_img)
gif.pack()
# Create two combo boxes for selecting GIFs and selecting playback modes
cbox1 = Combobox(values=('', 'blackline.gif', 'redline.gif', 'greenline.gif'), state='readonly')
cbox1.var = StringVar()
cbox1.configure(textvariable=cbox1.var)
cbox1.var.trace_add('write', lambda *args: gif.set_gif(r'../gif/' + cbox1.get()))
cbox1.pack(side=LEFT, expand=True)

cbox2 = Combobox(values=('click', 'display', 'hover'), state='readonly')
cbox2.var = StringVar(value='click')
cbox2.configure(textvariable=cbox2.var)
cbox2.var.trace_add('write', lambda *args: gif.set_play_mode(cbox2.get()))
cbox2.pack(side=LEFT, expand=True)

root.mainloop()

Result:

case_5.gif

Clone this wiki locally