0

I want to make my own canvas class to manage display, it add successfully to the view but won't show the image, it add text and shapes without a problem, though, so I thought it was a problem with the image, but the image works fine when I add it to a Label. So I tried a simple code, and it worked but whenever I put it inside a class it stop working.

Here's an example bellow, only canvas3 outside the class works fine, the other canvases add to the windows but won't display the image.

import tkinter as tk     # python 3
from tkinter import *
from PIL import Image, ImageTk, ImageDraw
# import Tkinter as tk   # python 2

class MyClass(): 
    def __init__(self, win):
        self.canvas= Canvas(win, width= 600, height= 200, bg="black")
        self.canvas.pack()
        img= ImageTk.PhotoImage(Image.open("download.png")) 
        self.canvas.create_image(10,10,anchor=NW,image=img)
        
        canvas2= Canvas(win, width= 600, height= 200, bg="black")
        canvas2.pack()
        img= ImageTk.PhotoImage(Image.open("download.png")) 
        canvas2.create_image(10,10,anchor=NW,image=img)
        

if __name__ == "__main__": 
    win = Tk() 
    win.geometry("750x800") 
    myobject=MyClass(win)
    # canvas.pack()
    canvas3= Canvas(win, width= 600, height= 200, bg="black")
    canvas3.pack()
    img= ImageTk.PhotoImage(Image.open("download.png")) 
    canvas3.create_image(10,10,anchor=NW,image=img)
    
    win.mainloop()
    
habibhassani
  • 486
  • 1
  • 6
  • 15

0 Answers0