How to Display and Play a GIF File in Python Tkinter GUIs
If you want to add some animation to your Python Tkinter GUIs, you can easily display and play a GIF file using the built-in PhotoImage
class in Tkinter. The process is simple and can add a nice touch to your application.
Step 1: Import the necessary modules
First, you need to import the tkinter
and PIL
modules. The PIL
module is used for working with images in Python.
<pre>
import tkinter as tk
from PIL import Image, ImageTk
</pre>
Step 2: Load the GIF file
Next, you need to load the GIF file using the Image.open
method from PIL and convert it to a Tkinter-compatible photo image.
<pre>
gif_path = "path_to_your_gif.gif"
gif = Image.open(gif_path)
photo = ImageTk.PhotoImage(gif)
</pre>
Step 3: Display the GIF on the tkinter window
Finally, you can create a label in your Tkinter window and set the photo as its image to display the GIF. You can also use the pack
or grid
methods to place the label in the desired location.
<pre>
root = tk.Tk()
label = tk.Label(root, image=photo)
label.pack()
root.mainloop()
</pre>
Step 4: Run and Animate the GIF
When you run your code, you should see the GIF displayed on the Tkinter window. If the GIF is an animated one, it will automatically start playing once it’s displayed.
Additional functionalities
You can also add buttons or other tkinter widgets to control the animation of the GIF, pause it, or even play it in reverse. This can be achieved by creating custom functions and binding them to events.
Conclusion
In this article, we have shown you how to easily display and play a GIF file in Python Tkinter GUIs. Adding animation to your GUIs can make them more attractive and engaging for the users, and with Tkinter and PIL, it’s a simple process to achieve.
💻 Reusable Source Code: https://www.buymeacoffee.com/fabiomusanni/e/182946
⬇️ LEARN ON THE BEST LEARNING PLATFORMS (LINKS BELOW) 😉💪 ⬇️
☕ Buy me a coffee: https://www.buymeacoffee.com/fabiomusanni
❤️ Support me monthly: https://www.patreon.com/FabioMusanni
😍 One-off donation: https://www.paypal.me/FabioMusanni/
SKILLSHARE
(Python, Web Dev, UI/UX Design, Music, Art, Animation and a lot more)
🔗 https://skillshare.eqcm.net/5gxzD2 (Affiliate)
DATACAMP
(Python, ChatGPT, SQL, Power BI, and a lot more)
🔗 https://datacamp.pxf.io/vN1bDj (Affiliate)
COURSERA PYTHON
(For beginners, Data Science, Data Analysis, AI, Cybersecurity and a lot more):
🔗 https://imp.i384100.net/k0Nk60 (Affiliate)
COURSERA WEB DEVELOPMENT
(Full Stack, Front-End, Back-End, Web Design and a lot more):
🔗 https://imp.i384100.net/EKWxBW (Affiliate)
Thank you for the support!❤
🎥All videos about Tkinter: https://www.youtube.com/playlist?list=PLs8qUrmRvaR1M1AatvOUy3eF_yoVEBJXk
🎥All my videos about Python: https://www.youtube.com/playlist?list=PLs8qUrmRvaR0IT4IwJl-LSweAdACW-yLK
This must have been the best explanation I have had and I was able to get my gif playing very nicely but how do I stop it?
I use it to display a loadingbar while another function is running via multithreading and want it to stop when the other function ends.
Amazing Sir