How to hide / show a GIF image in Tkinter - python

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).

You should do .itemconfig(Your Item Name, state=HIDDEN) and to show, change HIDDEN to NORMAL to show.

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()

Related

Tkinter - how to change icon color instead of using icon?

instead of using a ico file or blank icon in my tkinter application, i want the tkinter icon to be a 16x16 square with a certain color.
my current code:
import tkinter as tk
from tkinter import *
root = tk.Tk()
icon=PhotoImage(height=16, width=16)
icon.blank()
root.wm_iconphoto('True', icon) #New Tk 8.6 style
root.geometry('707x267')
root.title(" ")
root.mainloop()
how do i change the color of the icon?
You can create your own image with an image processing library, like PIL. First install it:
pip install Pillow
Then create a new image with:
from PIL import Image, ImageTk
img = Image.new(mode='RGB',size=(16,16),color='red')
Now convert the image into tkinter understandable format with:
img_tk = ImageTk.PhotoImage(img)
Next, set the icon to be that new image:
root.wm_iconphoto('True', img_tk)

Image, PPM, appearing black

When uploading an image to a label in Tkinter, it appears black. Not white or blank like some other questions, I did check them, and although I may have missed something, none of the answers there worked for me.
Essentially, I upload an image. It appears, however, it displays only a black image, instead of the image I was using. Here is my code:
#Imports are here
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from PIL import Image, ImageTk
#Retrieving and opening image
img = ImageTk.PhotoImage(Image.open("AFile.ppm"))
label = Label(frametwo, image=img)
label.photo = img
label.grid(column=2,row=2)
This does not display an errors, the image just shows up weird. I have no clue as to why. Thanks in advance.

I need a little help wth using tkinter and pillow(Python)

I am new to coding so problem might be very simple but I just dont get it.
So I am trying to upload a gif image to tkinter label. Here is my code:
from PIL import *
import PIL.Image
import tkinter as tk
from tkinter import *
root = Tk()
img = PIL.Image.open ("10.gif")
tkimage = PhotoImage(img)
label = Label(root, image=tkimage)
label.pack()
root.mainloop()
And here is the error that I am getting:
__str__ returned non-string (type GifImageFile)
I dont seem to find answer from google for this particular problem(I have tried)

How can I display an image using Pillow?

I want to display a gif image using Pillow
Here is my simple code:
from tkinter import *
from PIL import Image, ImageTk
import tkinter as Tk
image = Image.open("Puissance4.gif")
image.show()
But nothing happens...
All help will be appreciated
Thanks!
PIL provides a show method which attempts to detect your OS and choose an
appropriate viewer. On Unix it tries calling the imagemagick command display
or xv. On Macs it uses open, on Windows it uses... something else.
If it can't find an appropriate viewer, ImageShow._viewers will be an empty list.
On Raspbian, you'll need to install an image viewer such as display, xv or fim. (Note a search on the web will show that there are many image viewers available.) Then
you can tell PIL to use it by specifying the command parameter:
image.show(command='fim')
To display an image in Tkinter, you could use something like:
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
img = Image.open("image.gif")
tkimage = ImageTk.PhotoImage(img)
tk.Label(root, image=tkimage).pack()
root.mainloop()

Python Insert Image to Tkinter Text Widget

Im creating a simple chat box in Python and I want to insert an image(emoticons) to a TKinter text widget. I have tried it using this code:
img = Image.open("icon.jpg")
self.bigText.insert(END, img) # bigText is the text widget
Output of the code above is
<PIL.JpegImagePlugin.JpegImageFile instance at 0x01AB5A30>
instead of the image.
I'm not 100% sure on this, but I think you need to use image_create. Something like:
self.bigText.image_create(END, image=img)
should do the trick.
I have made it using:
from Tkinter import *
from PIL import Image, ImageTk
self.myEmoticons.append(self.smiley)
self.bigText.image_create(END,image = self.myEmoticons[self.myEmoticonsCtr])
self.myEmoticonsCtr=self.myEmoticonsCtr + 1

Categories

Resources