0

This works as expected:

img = tk.PhotoImage(file="gfx/button.png")
btn1 = tk.Button(root,image=img,width=wid,height=hgt)
btn1.grid(column=0,row=0,padx=pad_x,pady=pad_y)`  
btn2 = tk.Button(root,image=img,width=wid,height=hgt)
btn2.grid(column=1,row=0,padx=pad_x,pady=pad_y)

This does not - neither button gets an image:

btn1 = tk.Button(root,image=tk.PhotoImage(file="gfx/button.png"),width=wid,height=hgt)
btn1.grid(column=0,row=0,padx=pad_x,pady=pad_y)  
btn2 = tk.Button(root,image=tk.PhotoImage(file="gfx/button_active.png"),width=wid,height=hgt)
btn2.grid(column=1,row=0,padx=pad_x,pady=pad_y)

This works as expected:

img = tk.PhotoImage(file="gfx/button.png") 
btn1 = tk.Button(root,image=img,width=wid,height=hgt) 
btn1.grid(column=0,row=0,padx=pad_x,pady=pad_y)
img2 = tk.PhotoImage(file="gfx/button_active.png")  
btn2 = tk.Button(root,image=img2,width=wid,height=hgt) 
btn2.grid(column=1,row=0,padx=pad_x,pady=pad_y) 

This does not - second button gets correct image, first button gets no image:

img = tk.PhotoImage(file="gfx/button.png")
btn1 = tk.Button(root,image=img,width=wid,height=hgt) 
btn1.grid(column=0,row=0,padx=pad_x,pady=pad_y) 
img = tk.PhotoImage(file="gfx/button_active.png") 
btn2 = tk.Button(root,image=img,width=wid,height=hgt) 
btn2.grid(column=1,row=0,padx=pad_x,pady=pad_y)

I am particularly confused by the second example.

Clarification:

Why does this work:

img = tk.PhotoImage(file="gfx/button.png")
btn1 = tk.Button(root,image=img)

but this does not:

btn1 = tk.Button(root,image=tk.PhotoImage(file="gfx/button.png"))
Steve
  • 1
  • 1
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 30 '22 at 10:41
  • Does this answer your question? [Python Tkinter PhotoImage](https://stackoverflow.com/questions/17760871/python-tkinter-photoimage) – 0x5453 Mar 30 '22 at 19:25

0 Answers0