1

This is my code that I had written, I had fixed previous errors with help from other users. However this one stumps me. This is my code

I placed comment markers around the area I believe is the issue. To fix this problem I had read through a few sources however, it hasn't turned up any results.

import tkinter as tk
import sys
from timeit import default_timer as timer
import time
from PIL import ImageTk, Image



tot_time=0
del_time=0
imgpath=r'logo.jpg'

def NewTab():
    start=timer()
    time.sleep(del_time) #user defined delay time
    window.withdraw()
    win2.deiconify()
    end=timer()
    tot_time=end-start

    labW2.configure(text=('That took %s miliseconds or %s seconds'%(str(round(tot_time*1000,5)),str(round(tot_time,5)))))

def close():
    sys.exit()

def NTButt():
    win2.withdraw()
    window.deiconify()


win2=tk.Tk()
win2.geometry('1100x900')
win2.title('Button Click')
labW2=tk.Label(win2, text='ERROR: Time could not be calculated')
butW2=tk.Button(win2, text='Go back', bg='White', command=NTButt)
btn2W2=tk.Button(win2, text='Leave', bg='Red', command=close)
labW2.pack()
butW2.pack()
btn2W2.pack()
win2.withdraw()



window=tk.Tk()
window.geometry('1100x900')
window.title('Hello World')
lab1= tk.Label(window, text='Input the desired delay time')
ent=tk.Entry(window)
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))
photo = ImageTk.PhotoImage(imgset) 
labimg = tk.Label(window, image=photo)
labimg.image = photo 
labimg.pack()
###############################################
lab1.pack()
ent.pack()
btn.pack()
btn2.pack()

window.mainloop()

Ill include the error message I received

Traceback (most recent call last):
  File "C:\PythonScripts\trunk\Personal\PythonWindow_ForTiming.py", line 53, in <module>
    photo = ImageTk.PhotoImage(imgset) 
  File "C:\Python34\lib\site-packages\PIL\ImageTk.py", line 106, in __init__
    mode = Image.getmodebase(mode)
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 290, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "C:\Python34\lib\site-packages\PIL\ImageMode.py", line 50, in getmode
    return _modes[mode]
KeyError: <PIL.ImageTk.PhotoImage object at 0x0000000002A06208>
Exception ignored in: <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x0000000002DC45C0>>
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\PIL\ImageTk.py", line 116, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

I am new to tkinter and this may just be a error on my part, please comment solution to help me understand. Thanks in advance

1 Answer 1

1

I'm no TKinter expert, but it seems that the line which is raising the exception: photo = ImageTk.PhotoImage(imgset) Is not needed; You've already loaded the image in the previous line.

The exception is raised because you are passing in an instance of ImageTk.PhotoImage() to ImageTk.PhotoImage()

So, the bit of code in question becomes:

###############################################
photo = ImageTk.PhotoImage(Image.open(imgpath))
labimg = tk.Label(window, image=photo)
labimg.image = photo 
labimg.pack()
###############################################
Sign up to request clarification or add additional context in comments.

1 Comment

So I did this and got a new error as a result, _tkinter.TclError: image "pyimage1" doesn't exist ... Would you also know about this? Thanks again for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.