cannot associate image to tkinter label - python

I am trying to display an image to a tkinter GUI using tkinter.Label() widget. The procedure seems simple and straightforward, but this code doesn't work!
code:
import Tkinter as tk
import Image, ImageTk, sys
filename = 'AP_icon.gif'
im = Image.open(filename) # Image is loaded, because the im.show() works
tkim = ImageTk.PhotoImage(im)
root = tk.Tk()
label = tk.Label(root, image = tkim) # Here is the core problem (see text for explanation)
label.image = tkim # This is where we should keep the reference, right?
label.grid (row = 0, column = 0)
tk.Button(root, text = 'quit', command = lambda: sys.exit()).grid(row = 1, column = 1)
root.mainloop()
When we execute this code, it doesn't compile, giving an error:
TclError: image "pyimage9" doesn't exist
When I define label without its parent root, No compilation error occurs, but the GUI does not display any image!
Can anyone identify what could be the issue?

This problem happens when we attempt to run the above code in Ipython. And it can be solved by changing the line
root = tk.Tk() to
root = tk.Toplevel()

You need to create the root widget before you call any other tkinter functions. Move the creation of root to be before the creation of the image.

The general way which I use to display an image in tkinter is:
import Tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage(file = 'name of image.gif')
# If image is stored in the same place as the python code file,
# otherwise you can have the directory of the image file.
label = tk.Label(image = image1)
label.image = image1 # yes can keep a reference - good!
label.pack()
root.mainloop()
In the above case it works but you have something like:
import Tkinter as tk
image = tk.PhotoImage(file = 'DreamPizzas.gif') #here this is before root = tk.Tk()
root = tk.Tk()
# If image is stored in the same place as the python code file,
# otherwise you can have the directory of the image file.
label = tk.Label(image = image)
label.image = image
label.pack()
root.mainloop()
this gives me a runtime error: too early to create image.
but you have said that your error is image pyimage9 doesn't exist, this is strange because at the top you have set filename to 'AP_icon.gif', so you would think that you get a different error as I dont know where pyimage9 comes from. This makes me think that maybe you are getting the file name incorrect somewhere? You also need to move root = tk.Tk() to the top under imports.

Restart the Kernel to get rid of the error "TclError: image "pyimage9" doesn't exist"

Try the following code as I am able to rectify the same error:
window=Tk()
c=Canvas(window,height=2000,width=2000)
p=PhotoImage(file='flower1.gif',master = c)
c.create_image(500,500,image=p)

Related

How do I update pictures using Tkinter?

I've been having a problem with updating a picture in python using Tkinter. This program is creating a QR code and is displaying it on the window. I have no idea how to update it when it changes, the file name stays the same it just changes.
This is what activates the creation of a QR code
def GenerateQRCode():
# qr code maker 3000
qr = qrcode.QRCode()
qr.add_data("brush")
img = qrcode.make(input.get())
img.save("qrcode.png")
resize_image = img.resize((150, 150))
img2 = ImageTk.PhotoImage(img)
label1 = Label(root, image=img2)
label1.image = img2
label1.pack(pady=50)
It does the job of creating the QR code and dispalying it, however, like I said, no clue how to update it while the file name would stay the same. I could make qrcode1.png, then if new QR code is requested, check if it exists, if so, delete it and make qrcode2.png and display it, viceversa. But I'm sure there is a way how to do it with just one file and maybe even creating the file might be unnecessary. Any comment is welcome. Thank you.
You should create the label with the qr code once, and then update the label whenever you create the new qr code. Here's an example based off of your code:
import tkinter as tk
import qrcode
from PIL import ImageTk
def GenerateQRCode():
global qr_image
qr = qrcode.QRCode()
qr.add_data("brush")
img = qrcode.make(input.get())
img.save("qrcode.png")
resize_image = img.resize((150, 150))
qr_image = ImageTk.PhotoImage(img)
qr_label.configure(image=qr_image)
root = tk.Tk()
input = tk.Entry(root)
qr_image = tk.PhotoImage()
qr_label = tk.Label(root, image=qr_image, width=300, height=300)
button = tk.Button(root, text="Generate code", command=GenerateQRCode)
input.pack(side="top", fill="x")
qr_label.pack(side="top")
button.pack(side="bottom")
root.mainloop()

Tkinter Photo Image Issues

I am trying to run sample code and I am running into a problem where sublime says "_tkinter.TclError: couldn't recognize data in image file "C:\Users\atave\Dropbox\Python\tkinter Python Tutorial\Labels\prac.png". Can anyone tell me why? I have already set up sublime as an exception in the firewall, does it have to do with the location of the image?
import tkinter as tk
from tkinter import ttk
# create the root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('Label Widget Image')
# display an image label
photo = tk.PhotoImage(file="C:\\Users\\atave\\Dropbox\\Python\\tkinter Python Tutorial\\Labels\\prac.png")
image_label = ttk.Label(
root,
image=photo,
padding=5
)
image_label.pack()
root.mainloop()
the tk.PhotoImage doesn't work that well with .png files. I would suggest conversion to .bmp file or using PIL(Pillow) module- tk.PhotoImage(ImageTk.PhotoImage(file="C:\\Users\\atave\\Dropbox\\Python\\tkinter Python Tutorial\\Labels\\prac.png")
The ImageTk in ImageTk.PhotoImage() is imported from PIL(from PIL import ImageTk)

How to make a file open by default with tkinter app?

I made a tkinter app for displaying images, and I was wondering if there's a way to make an image open by default with this app
At the moment if I try that, I get an error for some non declared icon file (this is the icon that appears near the name of the app at the top)
There's no real goal behin this Gui, I'm just experimenting and learning.
Thanks
Try this:
from PIL import Image, ImageTk
import tkinter as tk
from tkinter import filedialog
my_img = []
def FileImport():
file = filedialog.askopenfilename()
my_img.clear()
my_img.append(ImageTk.PhotoImage(Image.open(file)))
label1.config(image=my_img[0])
root= tk.Tk()
root.title('Main')
root.geometry('400x400')
label = tk.Label(root, text = "Browse", fg="purple")
label.pack()
button = tk.Button(root, text='See Image',fg="blue", command=FileImport)
button.pack()
my_img.append(ImageTk.PhotoImage(Image.open(your_first_image)))
label1 = tk.Label(root, image = my_img[0])
label1.pack(pady= 50)
root.mainloop()
When you run this:
After you tap to see different image from computer:
Hope It helps!

Randomly displaying images with one window with Tkinter

I am trying to display images in a directory randomly in a single window that changes every 3 seconds. I also want it to be cross platform, as I am developing in Windows, but it will run on linux.
Currently I have this working code that iterates through all the image files of a directory with a mouse click (Code Below)
import os, sys, Tkinter, Image, ImageTk
def button_click_exit_mainloop (event):
event.widget.quit()
root = Tkinter.Tk()
root.bind("<Button>", button_click_exit_mainloop)
root.geometry('+%d+%d' % (-5,-5)) #controls where the window is
#gets list of file names in certain directory. In this case, the directory it is in
dirlist = os.listdir('.')
for f in dirlist:
try:
image1 = Image.open(f)
root.geometry('%dx%d' % (image1.size[0],image1.size[1]))
tkpi = ImageTk.PhotoImage(image1)
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
root.mainloop() # wait until user clicks the window
except Exception, e:
pass
The way it does this however is when the mouse clicks on the window it calls a function to close the widget.
The problem I am having is how to call this function, or close the widget without an event. Any suggestions?
This is what I currently have. This doesn't work obviously because it is stuck in the root.mainloop(), but it shows what I have generally in mind (Code below)
import os, sys, Tkinter, Image, ImageTk, random
root = Tkinter.Tk()
root.geometry('+%d+%d' % (-5,-5)) #controls where the window is
#gets list of file names in certain directory. In this case, the directory it is in
dirlist = os.listdir('.') #might not be in order, CHECK!!!
while True:
randInt = random.randint(0, 1)
image = Image.open(dirlist[randInt])
root.geometry('%dx%d' % (image.size[0],image.size[1]))
tkpi = ImageTk.PhotoImage(image)
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=image.size[0],height=image.size[1])
root.mainloop()
time.sleep(3)
Thank you!
-Jonathan
EDIT: Response to Bryan Oakley:
I tried what you suggested, and this looks like the solution.
The function is being called every 3 seconds, and a window is being created, but the image is not being placed in the window.
Is it that I do not have access to the root? How do I gain access?
Here is what I have currently:
import os, sys, Tkinter, Image, ImageTk, random
def changeImage():
#gets list of file names in certain directory. In this case, the directory it is in
dirlist = os.listdir('.') #might not be in order, CHECK!!!
#get random image
randInt = random.randint(0, 1)
image = Image.open(dirlist[randInt])
#set size to show, in this case the whole picture
root.geometry('%dx%d' % (image.size[0],image.size[1]))
#Creates a Tkinter compatible photo image
tkpi = ImageTk.PhotoImage(image)
#Put image in a label and place it
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=image.size[0],height=image.size[1])
# call this function again in three seconds
root.after(3000, changeImage)
root = Tkinter.Tk()
root.geometry('+%d+%d' % (-5,-5)) #controls where the window is
changeImage()
root.mainloop()
Thank you!!
SOLUTION EDIT:
I did not change the code so the label is only created once, so a label is created with each call. I did not do this because this could be applied to many other variables (dirlist = os.listdir('.') for exmaple), but would make the code harder to read. I did not see any disadvantage other than maybe more cycles used? I did not see a memory increase over time, which is all that mattered to me.
Here is the code, thank you Bryan Oakley for helping me!!
import os, Tkinter, Image, ImageTk, random
def changeImage():
global tkpi #need global so that the image does not get derefrenced out of function
#gets list of file names in certain directory. In this case, the directory it is in
dirlist = os.listdir('.')
#get random image
randInt = random.randint(0, 1)
image = Image.open(dirlist[randInt])
#set size to show, in this case the whole picture
root.geometry('%dx%d' % (image.size[0],image.size[1]))
#Creates a Tkinter compatible photo image
tkpi = ImageTk.PhotoImage(image)
#Put image in a label and place it
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=image.size[0],height=image.size[1])
# call this function again in 1/2 a second
root.after(500, changeImage)
tkpi = None #create this global variable so that the image is not derefrenced
root = Tkinter.Tk()
root.geometry('+%d+%d' % (-5,-5)) #controls where the window is
changeImage()
root.mainloop()
You need to remove your infinite loop -- tkinter already has one built-in. Instead, use after to periodically call a function:
def changeImage():
<do whatever you want, such as swapping out images>
# call this function again in three seconds
root.after(3000, changeImage)
Then, in your main program you would call the function before calling mainloop:
root = Tkinter.Tk()
...
changeImage()
root.mainloop()
Also, you don't need changeImage to create a new label widget every time -- create the label once outside the function, and just change the image each time it is called.
All credit goes to BryanOakley for finding the root cause of the error. I cleaned up your code slightly. The imports also changed a little since I'm running Python3, but wanted to make sure it would work.
import os, sys, tkinter, random # N.B. tkinter not Tkinter in py3
from PIL import Image, ImageTk # these are submodules in pillow py3
class Root(tkinter.Tk): # I buried this in a class. I prefer that for tkinter
def __init__(self):
super().__init__() # call Tk.__init__
self.CYCLEDELAY = 3000 # 3 second per cycle
# create and place the label once.
self.image_label = tkinter.Label(self)
self.image_label.place(x=0,y=0)
# call our function
self.changeImage()
def changeImage(self):
"""Change the image on a delay"""
dirlist = os.listdir('.')
image = Image.open(random.choice(dirlist))
# you had a funky method of getting a random member here. I cleaned it up
i_width, i_height = image.size
self.geometry("{}x{}".format(i_width,i_height))
# change root's geometry using string formatting (preferred)
self.image_label.configure(width=i_width, height=i_height)
# change the label's geometry using string formatting
self.tkpi = ImageTk.PhotoImage(image)
self.image_label.configure(image=self.tkpi)
# configure the label to use the PhotoImage
self.after(self.CYCLEDELAY,self.changeImage)
# loop!
root = Root()
root.mainloop()

empty Tkinter window appears in place of image

I am trying to load images dynamically through browse button to Tkinter window but I am getting the empty window. This is the code of callback function of browse button
supformats = [
('Windows Bitmap','*.bmp'),
('Portable Network Graphics','*.png'),
('JPEG ','*.jpg'),
('CompuServer GIF','*.gif'),
]
filename = askopenfilename(filetypes=supformats)
FILENAME = filename
im=Image.open(FILENAME)
w=im.size[0]
h=im.size[1]
root = Tkinter.Tk()
#canvas = Tkinter.Canvas(root, width=w, height=h)
#canvas.grid(row=0,column=0)
#tk_img = ImageTk.PhotoImage(file = FILENAME)
#canvas.create_image(image=tk_img)
im.show()
root.mainloop()
Thanks in advance for everyone who will help
From effbot's explanation http://effbot.org/tkinterbook/photoimage.htm Note that the file is accessed once only, not twice. This assumes you are using the Python Imaging Library in Tkinter as you did not state what image toolkit is being used.
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
You can use a PhotoImage instance everywhere Tkinter accepts an image object.
An example:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()

Categories