1

I'm trying to create Images in canvas using python class. but when I run the code, Image does not appear. Here is my code :


import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from PIL import Image,ImageTk

CompaniesFrame = ttk.LabelFrame(root, text =" Companies ")
CompaniesFrame.place(x=500, y=50)
CompaniesCanvas = ttk.Canvas(CompaniesFrame, width=900, height=500)
CompaniesCanvas.pack()


CompanyPhoto = "Company.png"
class CompanyIconClass:
    def __init__(self,CompanyName,ImagePath):
        self.CompanyName = CompanyName
        self.ImagePath = ImagePath

    def CreatImage(self):
        def OpenCompany(event):
            pass
        CompanyImage = ttk.PhotoImage(file = self.ImagePath)
        CompanyImage = CompanyImage.subsample(4)
        CompanyElement = CompaniesCanvas.create_image(150, 150, image=CompanyImage)
        CompaniesCanvas.tag_bind(CompanyElement, "<Button-1>", OpenCompany)


CompanyIconClass("Img1",CompanyPhoto).CreatImage()

Code works fine when I'm creating Image outside of the python class.

I would be grateful if anyone could help fixing the code.

zmaria
  • 21
  • 4
  • 2
    This code is missing some critical pieces and won't run as-is. Please try to [edit] it into a [mre] so people can test it out and try to help. At a guess, this is an issue with aggressive garbage collection, which often causes problems with images. But it's hard to say without a working example. – JRiggles Aug 16 '23 at 19:50
  • 2
    Agree with @JRiggles that it's likely garbage collected. Specifically, `CompanyImage` goes out of scope at the end of `CreatImage`. Try changing that to an instance variable. – Junuxx Aug 16 '23 at 19:55

1 Answers1

0

Try this code below. One important note, you did not have a root object or a mainloop() function to start the window. Another issue is that the companies frame was larger than the window, and so you had to expand the window manually in order to see the outlined box. Consider changing your place() calls and widths/lengths so everything fits together.

Per the comments, you should also make your code into a minimal reproducible example for people to better help you. As you see, I have significantly changed your code in order to get it to run, but this implementation may not match what you had in mind.

import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from PIL import Image,ImageTk

root = tk.Tk()

CompaniesFrame = ttk.LabelFrame(root, text =" Companies ")
CompaniesFrame.place("place where you want")
CompaniesCanvas = ttk.Canvas(CompaniesFrame, "SPECIFY DIMENSIONS")
CompaniesCanvas.pack()


CompanyPhoto = "Company.png"

class CompanyIconClass:
    def __init__(self,CompanyName,ImagePath):
        self.CompanyName = CompanyName
        self.ImagePath = ImagePath

    def CreatImage(self):
        file = Image.open(self.ImagePath)
        image = ImageTk.PhotoImage(file)
        label = tk.Label(image=image)
        label.image = image
        label.place("Place where you want")


photo = CompanyIconClass("Img1",CompanyPhoto)
photo.CreatImage()

root.mainloop()
teddyzhng
  • 80
  • 7