0

It's my first post here, I also have a puzzlement about the code below. I don't have the image displayed next to the text, the button is properly displayed (p.s : why the height and width have to be adjusted again, for example the button without the picture has width = 22 and height = 2, when I put the picture to be visible I have 200 by 200, well it can be a bit smaller), I have the text displayed, but the picture is missing...Could it be from .grid?

bl_logo = Image.open('img/balance.png')
bl_logo = bl_logo.resize((25,25), Image.ANTIALIAS)
balance_img = ImageTk.PhotoImage(bl_logo)
buttons_frame = tk.Frame(main_frame, bg = "#004d4d")
balance_btn = tk.Button(buttons_frame, image = balance_img, compound = tk.LEFT, text = 'BALANCE', font = ('Bebas NEUE', 20), width = 220, height = 200)
balance_btn.grid(row = 0, column = 0, sticky = tk.W, padx = 10, pady = (60,40)
codester_09
  • 5,622
  • 2
  • 5
  • 27
C Marian
  • 35
  • 5

1 Answers1

0

You have to save the balance_img in the widget class to prevent it from the garbage collector.

add this line balance_btn.image = balance_img

bl_logo = Image.open('img/balance.png')
bl_logo = bl_logo.resize((25,25), Image.ANTIALIAS)
balance_img = ImageTk.PhotoImage(bl_logo)
buttons_frame = tk.Frame(main_frame, bg = "#004d4d")
balance_btn = tk.Button(buttons_frame, image = balance_img, compound = tk.LEFT, text = 'BALANCE', font = ('Bebas NEUE', 20), width = 220, height = 200)
balance_btn.image = balance_img
balance_btn.grid(row = 0, column = 0, sticky = tk.W, padx = 10, pady = (60,40)

I already answer this question's answer to another user.

codester_09
  • 5,622
  • 2
  • 5
  • 27
  • Thank you very much! Can you also tell me why it is necessary to change the width and height? – C Marian Sep 09 '22 at 19:01
  • @CMarian size of the master widget(in this case `main_frame`) is depends on the child widgets so you have to change the size of your widget. – codester_09 Sep 09 '22 at 19:03
  • 1
    Thanks for the info, I'm just starting out so I'm sorry if I sound like a mess – C Marian Sep 09 '22 at 19:09