python tkinter image as button

Viewed 16

I'm using tkinter library to design UI. I'm trying to use my own images as buttons that run function. Everything else seems to be working but images are not displayed, just regular buttons. I'm kind of stuck now.

def load_buttons(self):
    files = []
    for i in range(5):
      files.append(f'{str(i)}.PNG')
    for i in range(len(files)):
      img = PhotoImage(file=files[i])
      self.buttons.append(Button(self.root,
                                 image=img,
                                 text=str(i),
                                 command=lambda c=i: print(self.buttons[c].cget("text"))))
      self.buttons[i].pack()
0 Answers
Related