-1

I have written a basic application in Tkinter. Recently, I have started to re-write the application in a class-based format. However, I am having some issues loading images, which did not occur when my code did not involve classes.

My code takes the form:

import tkinter as tk 
from PIL import ImageTk, Image 

class MainWindow:
    def __init__(self, master):
        self.master = master
        self.master.title("Title")
        self.master.iconbitmap('icon')
        self.master.geometry('500x500')

        image=Image.open('GGlogo.jpg')
        image=imga.resize((200,200), Image.ANTIALIAS)
        render = ImageTk.PhotoImage(imga)
        
        self.label_img = tk.Label(self.master, image=render)
        self.label_img.grid(row=1, column=1, padx=10, pady=10)

        self.test_button = tk.Button(self.master, image=img, text='text')
        self.test_button.grid(row=2, column=1)

        

def main():
    root = tk.Tk()
    main_window = MainWindow(root)
    root.mainloop()

if __name__ == '__main__':
    main()

However, when I run the application, no image is shown. I also tried to load the image into a tk.Button object, yet it still did not show. This is shown as test button in the code. It seems I am having a problem loading an image, but I can't figure out what.

Program Output - Image displays what the program outputs.

Thanks for any help.

1 Answers1

0

change image to self.image and render to self.render. that might do the work.

however, I suggest to use it like this:

        #image_directories
        self.images = {
            "logo": self.ready_img("logo.png", (120, 130))
        }

        def ready_img(self, path, size): 

            return ImageTk.PhotoImage(Image.open(path).resize(size, Image.ANTIALIAS))

then to get the image you can do : self.images.get("logo")

I was facing the same problem, doing like this solved it.

fayez
  • 370
  • 1
  • 5