简体   繁体   中英

How to hide / show a GIF image in Tkinter

In using Python's Tkinter application, I have come across a little problem. How do I hide and show GIF images that I have put into the window using PhotoImage?

I can make the images appear in the first place, but am unable to do anything with them. I have tried using canvas.itemconfig and canvas.update, but to no avail. Is anyone able to solve this problem?

picture = PhotoImage(file='C:\\Users\\ZecFamily5\\Downloads\\Island.gif')
c.create_image(250, 250, image=picture)

The above code cannot be hidden or shown using c.itemconfig(image, state=HIDDEN).

您应该执行.itemconfig(Your Item Name, state=HIDDEN)并显示,将HIDDEN更改为NORMAL以显示。

I would recommend using the ImageTK module:

img = ImageTk.PhotoImage(Image.open(path))

As it should allow you to make a TKinter compatible photo image. For this to work though, do keep in mind that this will need to be at the beginning of your file:

import Tkinter as tk
from PIL import ImageTk, Image

Here is an example piece of code to show you what I mean:

import Tkinter as tk
from PIL import ImageTk, Image

path = 'C:/xxxx/xxxx.jpg'

root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM