I am trying to make a tkinter code that can generate a window with an image on it. This is the area that keeps giving me an error:
window=tk.Tk()
window.geometry('1100x900')
window.title('Hello World')
lab1= tk.Label(window, text='Input the desired delay time')
btn=tk.Button(window, text='Go to new window', bg='Blue', command=NewTab)
btn2=tk.Button(window, text='Leave', bg='Red', command=close)
imgset=ImageTk.PhotoImage(Image.open(imgpath))
img = tk.Label(window, image=imgset)
img.pack()
lab1.pack()
btn.pack()
btn2.pack()
window.mainloop()
where imagepath is a path to a picture in the my pictures folder
and this is the error I keep getting
Traceback (most recent call last):
File "C:\PythonScripts\trunk\Personal\PythonWindow_ForTiming.py", line 49, in <module>
img = tk.Label(window, image=imgset)
File "C:\Python34\lib\tkinter\__init__.py", line 2604, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 2122, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist
What am I doing incorrectly? Could you please include comments to help me understand, I am just learning about tkinter
Thanks in advance
You need to add 2 lines as following (I commented the lines in question)
imgset=Image.open(imgpath)
# Convert imgset to a Tkinter-compatible image object
photo = ImageTk.PhotoImage(imgset)
img = tk.Label(window, image=photo)
# Keep a reference to the image
img.image = photo
img.pack()
Elementary note: you may rename img to something that reflects the Label() instance better (to avoid an eventual confusion) such as label
You may be interested in reading The Tkinter PhotoImage Class
Related
r_login = Tk()
r_login.title("Admin Login - Student Management System")
r_login.geometry("1280x720")
bg2 = PhotoImage(file="002.png")
lbl_bg2 = Label(r_login, image=bg2)
lbl_bg2.pack()
icon = PhotoImage(file='logo.png')
r_login.iconphoto(True, icon)
root = Tk()
root.title("Student Management System")
root.geometry("1280x720")
bg1 = PhotoImage(file="003.png")
lbl_bg1 = Label(root, image=bg1)
lbl_bg1.pack()
icon = PhotoImage(file='logo.png')
root.iconphoto(True, icon)
title = Label(root, text="Student Management System",
font=("Arial", 48, "bold"),
fg="black", bg="white")
title.place(x=226, y=30)
Output:
Traceback (most recent call last):
File "D:\Students Management\main.py", line 137, in <module>
lbl_bg1 = Label(root, image=bg1)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\amicr\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3214, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\amicr\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 2628, in __init__
self.tk.call(
_tkinter.TclError: image "pyimage3" doesn't exist
I'm not being able to display the image in next. The first one is for admin for login and where the second is for another
Both of this image in my working directory and logo also. But some how I'm getting this error
You have more than one instance of Tk. All of the images are being created in the first instance and cannot be used in the second or subsequent instances.
You should not be creating two instances of Tk. Instead, if you need multiple windows. The second and subsequent windows should be instances of Toplevel.
For more info for why multiple instances of Tk is discouraged, read this
I'm doing with tutorial on youtube in this Python Course at 6:23:00 for him is working I don't know what to do.
Code:
from tkinter import *
#label = an area widget that holds text and/or an image within a window
window = Tk()
photo = PhotoImage(file='C:\\Users\\Kuba\\Desktop\\folder\\images\\3x')
label = Label(window, text='something', font=('Arial',30,'bold'), fg='#00FF00',
bg='black', bd=10, relied=RAISED, padx=20,pady=20, image=photo)
label.pack()
#label.place(x = 0,y= 0)
window.mainloop()
Result:
Traceback (most recent call last):
File "c:\\Users\\Kuba\\Desktop\\folder\\programowanie\\nauka programowania python\\nauka po angielsku\\nauka66.py", line 8, in \<module\>
photo = PhotoImage(file='C:\\Users\\Kuba\\Desktop\\folder\\images\\3x')
File "C:\\Users\\Kuba\\AppData\\Local\\Programs\\Python\\Python310\\lib\\tkinter\__init_\_.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, \*\*kw)
File "C:\\Users\\Kuba\\AppData\\Local\\Programs\\Python\\Python310\\lib\\tkinter\__init_\_.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
\_tkinter.TclError: couldn't open "C:\\Users\\Kuba\\Desktop\\folder\\images\\3x": no such file or directory
As the error implies "C:\Users\Kuba\Desktop\folder\images\3x": no such file or directory, you either do not have the image or made a typo in the directory. you can change it manually using file explorer to make sure your photo exists in the directory you entered.
To change things up, I wanted to put blue wallpaper image as my background on my python script. Here is the code:
from tkinter import *
root = Tk()
root.title("")
root.iconbitmap()
#root.minsize(width=1370, height=800)
#root.maxsize(width=1370, height=800)
background = PhotoImage(file="background.jpeg")
background_label = Label(root,image=background)
background_label.place(x=0,y=0,relwidth=1,relheight=1)
root.mainloop()
However, when I run this code this error pops up:
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "background.jpeg"
Does anyone know how to solve this problem?
You Can Use ImageTk:
from PIL import ImageTk
from tkinter import *
root = Tk()
root.title("")
root.iconbitmap()
background = ImageTk.PhotoImage(file="background.jpeg")
background_label = Label(root,image=background)
background_label.place(x=0,y=0,relwidth=1,relheight=1)
root.mainloop()
Hope It Helps :D
I'm trying to create a label with a vertical scrollbar, using code similar to this:
from Tkinter import *
master = Tk()
master.geometry('480x320')
scrbar = Scrollbar(master, orient=VERTICAL, width=28)
scrbar.place(x=450, y=52)
lbl = Label(master, text=('A'*100), yscrollcommand=scrbar.set)
lbl.place(x=10, y=10)
scrbar.config(command=lbl.yview)
master.mainloop()
When running this, I get the following error:
Traceback (most recent call last):
File "test.py", line 6, in <module>
lbl = Label(master, text=('A'*100), yscrollcommand=scrbar.set)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2590, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2089, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-yscrollcommand"
Does anyone know a solution?
The solution is to use a widget that supports a scrollbar. The Label isn't such a widget. If you only need to scroll horizontally you can use an Entry widget and just set the state to disabled after setting the text.
If you need to scroll vertically, you can use a Text widget. You can also use a canvas with either a text item drawn on the canvas, or a label embedded in the canvas (search for how to do a scrollable frame; the solution is the same even though it's a label rather than a frame)
I have been trying to include images in my Tkinter widgets, but nothing seems to work. Here's my code:
from Tkinter import *
from PIL import Image
root = Tk()
image = Image.open('images/myimage.jpg')
root.image = image
b = Radiobutton(root, text='Image',image=image,value='I')
b.pack()
root.mainloop()
The error I get is:
Trac
eback (most recent call last):
File "C:/Users/.../loadimages.py", line 7, in <module>
b = Radiobutton(root, text='Image',image=image,value='I')
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2714, in __init__
Widget.__init__(self, master, 'radiobutton', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1974, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TclError: image "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=402x439 at 0x2A6B080>" doesn't exist
Most internet sources suggest keeping a reference to the image to avoid the garbage collector, but I don't see how I could do that more than what I have here. There are also suggestions regarding having multiple Tk instances, but I only have one.
To whoever helps, thanks in advance!
You need to convert the image you open using PIL into a PhotoImage:
from PIL import Image, ImageTk
image = Image.open("images/myimage.jpg")
photoimg = ImageTk.PhotoImage(image)
b = Radiobutton(root, image=photoimg)
See also: Photoimage API