pythontkintertkinter-layout

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


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


Solution

  • 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