Reputation: 1227
When trying to place an image in a button, I get an error "_tkinter.TclError: image "pyimage1" doesn't exist". The image does exist though because I used it on another button and it works there. When I try to use the image again on a second button that's when the error occurs.
I have tried removing the image and the button works. Tried using the image on anther button and it works for one button only.
from tkinter import ttk
from tkinter import Tk, PhotoImage
class Window(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
def main():
root = Tk()
style = ttk.Style(root)
style.theme_use('clam')
root2 = Tk()
style = ttk.Style(root2)
style.theme_use('alt')
root3 = Tk()
style = ttk.Style(root3)
style.theme_use('classic')
root4 = Window()
style = ttk.Style(root4)
style.theme_use('default')
icon = PhotoImage(file='test.gif')
# This line works, the image appears on the button.
ttk.Button(root, image=icon, compound='left', text="Quit", command=root.destroy).pack()
# This line works with out the image.
ttk.Button(root2, compound='left', text="Quit", command=root2.destroy).pack()
# This line does not work with an image.
# if the line below is un-commented the code does not work, the error I get is below.
# _tkinter.TclError: image "pyimage1" doesn't exist
# ttk.Button(root2, image=icon, compound='left', text="Quit", command=root2.destroy).pack()
ttk.Button(root3, text="Quit", command=root3.destroy).pack()
ttk.Button(root4, text="Quit", command=root4.destroy).pack()
root.mainloop()
if __name__ == '__main__':
main()
I want images on all buttons. I can only get it to work for one button only.
Upvotes: 0
Views: 1069
Reputation: 225
You're going to have to create another PhotoImage for each window with the master
keyword:
icon = PhotoImage(master=root, file='test.gif')
icon2 = PhotoImage(master=root2, file='test.gif')
icon3 = PhotoImage(master=root3, file='test.gif')
icon4 = PhotoImage(master=root4, file='test.gif')
And then use each icon for the corresponding button:
ttk.Button(root, image=icon, compound='left', text='Quit', command=root.destroy).pack()
ttk.Button(root2, image=icon2, text='Quit', command=root2.destroy).pack()
ttk.Button(root3, image=icon3, text='Quit', command=root3.destroy).pack()
ttk.Button(root4, image=icon4, text='Quit', command=root4.destroy).pack()
Hope this works for you.
Upvotes: 3