Gif image not working in tkinter - python

from tkinter import *
photo = PhotoImage(file="C:\Temp\test\computer.gif")
lbl = Label(root, image=photo, height="10", width="20").pack
I have absolutely no idea why this won't work it comes up with: _tkinter.TclError: couldn't recognize data in image file "C:\Temp\test\computer.gif.

Windows filenames always have to be entered as raw strings (in all of python, not just with tkinter). Also, you'll have to make a root window first. Try this:
from tkinter import *
root = Tk()
photo = PhotoImage(file=r"C:\Temp\test\computer.gif")
Label(root, image=photo, height="10", width="20").pack()
root.mainloop()

Related

Images in python using tkinter

Can't make this image to display. I get an error saying TclError: image "pyimage21" doesn't exist
Here is what I tried
import tkinter as tk
root = tk.Tk()
root.geometry("500x500")
root.title("Poker ranges")
photo = tk.PhotoImage(file = "C:\\Users\\chaeh\\OneDrive\\Desktop\\poker\\hand_range_grid.png")
text = tk.Label(root,image = photo)
root.mainloop()
You need to pack, grid or place your label...
I suggest to use pathlib to find correct path, because tkinter need relative references to use images...
import pathlib
import tkinter as tk
DIR = pathlib.Path('.')
root = tk.Tk()
root.geometry("500x500")
root.title("Poker ranges")
photo = tk.PhotoImage(file=DIR / "abc.png")
text = tk.Label(root, image=photo)
text.pack()
root.mainloop()

If in the code there is ImageTk I can not open the .py file with python application

I have tried to open this program with double click using python application but it doesn't work.
I test it and I think the problem is the use of ImageTk.
from tkinter import *
from PIL import ImageTk,Image
root = Tk()
root.title('Images')
root.iconbitmap('G_image.ico')
my_img = ImageTk.PhotoImage(Image.open('2021_1.png'))
my_label = Label(image=my_img)
my_label.pack()
button_quit = Button(root,text='Exit Program',command=root.destroy)
button_quit.pack()
root.mainloop()
Do you have any errors?
But I guess it's because you have to "pack" your label in the root window:
my_label = Label(root, image=img)
my_label.pack()
and to double click on the python file, try this:
right click on the file->open with->choose default program->more options->select python.exe file and click on.

Why can't I display an Image in a tkinter Toplevel() window?

I'm trying to create a python program tkinter that, upon the pressing of a button, opens a new full screen tkinter window containing an image and plays an audio file - here's my code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.png"))
label = Label(window, image=img).pack()
playsound("song.mp3")
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
(my image and audio file are both on the desktop with my python file)
However, when I run my code, when I press the button, the audio plays but no second tkinter window opens.
I've tried to destroy() the buttonWindow and have tried many different ways of including an image on a tkinter window - if I remove the line of code using PhotoImage(), the window appears (obviously I then get a syntax error stating that 'img' is not defined).
How could I solve this?
Thanks,
Louis
I had similar problem.
But i resolve this trial and error method.
First of all i don't use pillow library and put image into Label
You should try define Photoimage object NOT IN FUNCTION
Example:
It will work:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
def popUp():
popff = tk.Toplevel(root)
popff.geometry("400x200")
labelImgff = tk.Label(popff, image=imgff, bg="white")
labelImgff.grid(row=0, column=0)
imgff = tk.PhotoImage(file="/home/user/image.png")
btnff = tk.Button(text="startPOP", command=popUp)
btnff.grid(column=0,row=0)
root.mainloop()
effect of action
Your playsound() command is blocking execution. The playsound() command has an optional field 'block', which is True by default. Changing this to False will continue execution and allow mainloop() to continue.
Second, just call label.draw() to draw your image to the TopLevel window.
Here's the code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.jpeg"))
label = Label(window, image=img).pack()
playsound("song.mp3",block=False)
label.draw()
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
buttonWindow.mainloop()
Cheers!

How to display Url Image Tkinter python 3

How can I visualize an url of an image with tkinter ("http://images.amazon.com/images/P/0374157065.01.LZZZZZZZ.jpg").I would like to place it in the last column of the program, in standard size, I tried many guides but none of them ever worked. Thanks very mutch
Why not bind a URL to a Label like this?
from tkinter import *
import webbrowser
def callback(event):
webbrowser.open_new(r"http://www.your-image.com")
root = Tk()
link = Label(root, text="The image", fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", callback)
root.mainloop()

How can I open an image in Python?

I have looked at a lot of tutorials tried them all, but nothing seemed to work through Pygame, PIL, Tkinter. it could be because of me of course, cause Im a greenie...
from Tkinter import *
root = Tk()
photo = PhotoImage(file="too.jpg")
label = Label(root, image=photo)
label.pack()
root.mainloop()
Your code is correct but it won't work because of the jpgfile.
If you want to use the PhotoImage class you can only read read GIF and PGM/PPM images from files (see docs).
For other file formats you can use the Python Imaging Library (PIL).
Here's your example using PIL:
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
image = Image.open("too.jpg")
photo = ImageTk.PhotoImage(image)
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
root.mainloop()
The line label.image = photo is necessary if you want to avoid your image getting garbage-collected.

Categories

Resources