0

I have an email sender that uses some pictures created in a separate python file and I am trying to implement it into my main code. when it is run by itself it performs all of its attributes and also displays the images that have been put into it as buttons. But when putting the code into the main project I am met with this error:(_tkinter.TclError: image "pyimage9" doesn't exist) even though the tkinter file and the pictures are in the same folder. This is a snippet of the code:

    attachImage=PhotoImage(file='attachments.png')

    Button(emailLabelFrame,text=' Attachment',image=attachImage,
           compound=LEFT,font=('arial',12,'bold'),
           cursor='hand2',bd=0,bg='dodger blue2',activebackground='dodger blue2',command=attachment).grid(row=0,column=1)
  • Please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Cannot understand error by the given snippet. Does [this](https://stackoverflow.com/questions/25460418/cannot-associate-image-to-tkinter-label) answer your problem? – Kartikeya Mar 13 '22 at 09:04
  • 1
    Most probably there are more than one instance of `Tk()`. – acw1668 Mar 13 '22 at 10:37
  • This might be an issue related to keeping an image reference, you can save the button as an object, then set the image attribute of the button to that image as well, the image object gets accidentally garbage collected, and thus this error is thrown. – typedecker Mar 13 '22 at 11:21
  • 1
    Yes, acw1668. the problem was that window=Tk() whereas when I changed it to window=Toplevel() it worked fine. – BigBadger789 Mar 13 '22 at 14:04

0 Answers0