self.tk.call( _tkinter.TclError: image "pyimage3" doesn't exist - python

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

Related

Tkinter error: image "pyimage2" doesn't exist working with classes and frames

i'm working with classes on tkinter and i have this problem:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 549, in _clicked
self._command()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\input_frame.py", line 88, in go_back
from main import SerialFrame
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 126, in <module>
SerialFrame(root).place(x=25, y=50)
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 20, in __init__
self.createWidgetsMain()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 101, in createWidgetsMain
refresh_serials = customtkinter.CTkButton(master=self, command=refresh_menu, image=my_image, width=20,
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 106, in __init__
self._draw()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 261, in _draw
self._update_image() # set image
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 172, in _update_image
self._image_label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1675, in configure
return self._configure('configure', cnf, kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1665, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
This is the code on my application and explanation about how it should work:
First of all, i have a file with the class SerialFrame, and the creation of the window and the frame:
class SerialFrame(customtkinter.CTkFrame):
# CONSTRUCTOR FOR THE FRAME
def __init__(self, master, *args, **kwargs):
super(SerialFrame, self).__init__(master)
self.master = master
self.serial_port = ""
self.configure(width=400, height=400)
self.createWidgetsMain()
# METHOD TO CREATE ALL WIDGETS
def createWidgetsMain(self):
...
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
back = backend.MyAppBackend()
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
And i have another 2 files with other diferent classes for other frames in similar way.
The problem is when i press a button to go back to the first frame, here is the code in the other classes:
def go_back():
self.destroy()
btn_back.destroy()
from main import SerialFrame
SerialFrame(self.master).place(x=25, y=50)
btn_back = customtkinter.CTkButton(self.master, text="Go Back",
command=go_back, cursor="hand2")
btn_back.place(x=465, y=400)
Obviously, while coding the app i had many different problems and if you see something that shouldn't be work well, you can tell me.
I think that probably the error would come here. This code is on def createWidgetsMain, on the main file, and the SerialFrame class.
my_image = customtkinter.CTkImage(light_image=Image.open("images/refresh.png"),
dark_image=Image.open("images/refresh.png"),
size=(20, 20))
# CREATE REFRESH BUTTON
refresh_serials = customtkinter.CTkButton(master=self, command=refresh_menu, image=my_image, width=20,
text="")
I think that when i press the go_back button, on the other classes, it should create a new object of SerialFrame class and place in the root.
Obviously, when i create the other frames, i always send the root, the Tk().
Here is the code of the button to go create the other classes (it's inside the createWidgedsMain method):
def segmented_button_callback(value):
if value == "Inputs":
self.destroy()
input_frame.InputFrame(self.master, back).place(x=75, y=75)
if value == "Menu":
try:
connection = back.get_connection()
self.destroy()
menu_frame.MenuFrame(self.master, back).place(x=25, y=75)
except:
self.destroy()
SerialFrame(self.master).place(x=25, y=50)
segemented_button = customtkinter.CTkSegmentedButton(master=self,
values=["Menu", "Inputs"],
command=segmented_button_callback)
All application works well, my only problem is that, thank you.
Here are some pics of the app
Since you have the following code inside main.py:
...
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
back = backend.MyAppBackend()
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
So when the line from main import SerialFrame inside go_back() is executed, another instance of customtkinter.CTk() will be created which causes the exception.
You need to put the code block inside a if __name__ == "__main__" block as below:
...
if __name__ == "__main__":
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
Then the code block will not be executed when main is imported.

Cannot open image in window

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.

How to place a jpeg into tkinter (python3.4)?

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

tkinter - Can't use a vertical scrollbar with a label

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)

Binding a Color to a Button in Tkinter Results in a TclError

I am currently trying to implement code into my program to update a buttons color when the user hovers the mouse cursor over it. The program recognizes the hover, but returns an error.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Users\oiest\Documents\Programs\iNTMI\v1.1.3b\iNTMI.py", line 252, in <lambda>
achievementsButton.bind("<Enter>", lambda event: achievementsButton.configure(bg = "red"))
File "C:\Python34\lib\tkinter\__init__.py", line 1270, in configure
return self._configure('configure', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 1261, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-bg"
I had Googled how to do the changing of colors when hovering and found the following code. Though for some reason, it does not work for me. What am I doing wrong?
achievementsButton.bind("<Enter>", lambda event: achievementsButton.configure(bg = "red"))
achievementsButton.bind("<Leave>", lambda event: achievementsButton.configure(bg = "white"))
This is the code from where I originally defined achievementsButton.
achievementsButton = ttk.Button(self, text = "Achievements", command = lambda: controller.show_frame(achievements), width = "25")
ttk.Button instances do not have a bg or background attribute. There are two solutions:
Use an ordinary tkinter.Button, which does have a bg attribute.
Keep using the ttk.Button, and configure it using a style object. See Using and customizing ttk styles for more information. Example:
from Tkinter import *
import ttk
root = Tk()
s = ttk.Style()
s.configure("regular.TButton", background="red")
s.configure("onhover.TButton", background="white")
button = ttk.Button(root, style="regular.TButton")
button.pack()
button.bind("<Enter>", lambda event: button.configure(style="onhover.TButton"))
button.bind("<Leave>", lambda event: button.configure(style="regular.TButton"))
root.mainloop()
However, this will only change the background color of the area behind the actual button, rather than the button's face. This
post seems to indicate that it's impossible to change the face color of a ttk Button.

Categories

Resources