Button Picture Attribute issues with Tkinter

I’m having weird issues with some iterated buttons that are made inside of a function. I have my button list as a global variable and photos are saved within my main python folder.

When I use the Button image= attribute, the buttons aren’t clickable and are kind of greyed out with no picture. When I use the Button text= attribute, the buttons are clickable and the text pops up, but the width and height attributes don’t run. I haven’t been able to find similar issues/answers here.

        from tkinter import *
        from tkinter import filedialog as fd
        from tkinter.messagebox import showinfo
        from PIL import Image, ImageTk

        def OnSomeClick():
        ---Some Code---
            if Orientation == 1:
                 for i in range(len(Ordered_xCoord)):
                 InitButtonPhoto = '{}.png'.format(i)
                 ButtonPhoto = PhotoImage(file=InitButtonPhoto)
                 ImageButtonList.append(Button(action_window, command=Command, bd=3, width=110,
                                          height=85, image=ButtonPhoto))
                 ImageButtonList[i].grid(row=Ordered_yCoord[i], column=Ordered_xCoord[i])
            #this top if statement is what runs with my test; the bottom is my original
            else:
                 for i in range(len(Ordered_xCoord)):
                 ImageButtonList.append(Button(action_window, 
                 image=PhotoImage(file='{}.png'.format(i)),
                                          command=Command, bd=3, width=85, height=110))
                 ImageButtonList[i].grid(row=Ordered_yCoord[i], column=Ordered_xCoord[i])

When loading the photo through PhotoImage(Image.open(file)), there is a traceback error. I’m not too sure why my photos are out of scope/garbaged or why the command working is dependent on the photo saving.

Traceback Error with Image.open:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Users/.conda/envs/CoordinateMath/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/Users/PycharmProjects/CoordinateMath/TkinterWindow.py", line 163, in Submit
    ButtonPhoto = PhotoImage(file=InitButtonPhoto)
  File "/Users/.conda/envs/CoordinateMath/lib/python3.8/tkinter/__init__.py", line 4064, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Users/.conda/envs/CoordinateMath/lib/python3.8/tkinter/__init__.py", line 4009, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=990x765 at 0x11034C310>": no such file or directory

Answer

Thank you, Matiiss and BryanOakley! The fix was a combo of the latest comment and putting my changing the formatting of my image list.

def onClick():
   global Image_List, Ordered_xCoord, Ordered_yCoord, ImagedButtonList
--some code---
   for i in range(len(Ordered_xCoord)):         
     y = myimage.save('{}.png'.format(i))
     z = ImageTk.PhotoImage(Image.open('{}.png'.format(i)))
     Image_List.append(z)
---more code----
   if PaperOrientation == 1:
        for i in range(len(Ordered_xCoord)):
            InitButtonPhoto = Image_List[i]
            ButtonPhoto = InitButtonPhoto
            ImageButtonList.append(Button(action_window, command=Command, bd=3, width=110,
                              height=85, image=ButtonPhoto))
            ImageButtonList[i].grid(row=Ordered_yCoord[i], column=Ordered_xCoord[i])
    else:
        for i in range(len(Ordered_xCoord)):
            InitButtonPhoto = Image_List[i]
            ButtonPhoto = InitButtonPhoto
            ImageButtonList.append(Button(action_window, command=Command, bd=3, width=85,
                              height=110, image=ButtonPhoto))
            ImageButtonList[i].grid(row=Ordered_yCoord[i], column=Ordered_xCoord[i])

Kind of sloppy on my part, but it works.