0

I am using tkinter to make a simple drawing tool. I am trying to put an image, fill_bucket.png, on the button. However, when I run the program, the button is incredibly small, like it has no content, and there is not image.

From looking around, it seems like the issue is probably Python's garbage collection, however I have tried many ways of fixing this and nothing seems to work. The simplest thing I have tried is:

self.fill_bucket_photo = PhotoImage(file='./img/fill_bucket.png')

self.fillButton = Button(self.button_layout, image=self.fill_bucket_photo, compound='top', font=('Arial', 25), width=1, height=1, command=lambda: self.selectTool('fill'))

However this does not work. I have also tried this solution, which I found here:

label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()

However when I do self.fillButton.image after assigning self.fillButton, I get an Attribute error for the image attribute.

Everything that I am doing is inside a class, and I can define normal buttons with text fine.

Additionally, doing

self.fill_bucket_photo = PhotoImage(file='./img/fill_bucket.png')
self.label = Label(image=self.fill_bucket_photo).grid(row=1, column=1)

shows the image normally.

Full example. On my computer, this code creates a window that contains one blank button with no image on it:

from tkinter import *

class App():
    def __init__(self):
        self.root = Tk()
        self.root.resizable(False, False)

        self.fill_bucket_photo = PhotoImage(file='./img/fill_bucket.png')

        self.fillButton = Button(self.root, image=self.fill_bucket_photo, compound='top', font=('Arial', 25), width=40, height=40).pack()

        self.root.mainloop()
        
        
app = App()

What are some possible solutions to this?

HuoKnight
  • 51
  • 4
  • It doesn't sound like a garbage collection issue. If you are getting an error you should post it. the full contents – Alexander Jan 31 '23 at 03:32
  • 1
    Well, you're *telling* the Button to be one pixel square - the units of `width` and `height` are pixels if an image is specified. I suspect that's your entire problem right there, and there isn't actually an image garbage collection problem. – jasonharper Jan 31 '23 at 03:36
  • That solves part of the issue, I had no idea that specifying an image changed how those options effected the Button. However I set the dimensions to 40x40 and there is still no image being drawn, so might it still be a garbage collection issue? Also, to clarify, I am not getting any errors. – HuoKnight Jan 31 '23 at 03:51
  • You need to provide a [mre]. We cannot identify the issue with just a few lines of code. – acw1668 Jan 31 '23 at 10:19
  • I have edited the original question to include this, thanks for the reminder! – HuoKnight Jan 31 '23 at 17:46
  • How big is the image, and does it have transparent sections? You are setting the button to an explicit size, have you tried removing the size to see the full image? Maybe the images are bigger than you think and you are seeing a transparent part of the image. – Bryan Oakley Jan 31 '23 at 22:36
  • That looks to be the issue. Don't know why I thought that the image size would effect the button size, thanks! – HuoKnight Feb 01 '23 at 01:49

0 Answers0