0

I am using the following to add a button to my GUI. I am not able to get the image to display. Without specifying height or width, the button is huge but no image. Can you tell me what I am doing wrong? All of my other buttons, created similarly but with no image are displayed fine.

photo = PhotoImage(file=r"C:\Users\jim\Pictures\refresh.png")
self.bt_plot_test1 = tk.Button(self.lf_FileFunctions, image = photo, command=self.plot_employee_allocations)
self.bt_plot_test1.grid(row=3, column=0, padx=0, pady=0,sticky=tk.NW)  

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Jim Rutter
  • 45
  • 4

1 Answers1

0

You can use the following code for your purpose:

#Import necessary modules
from tkinter import *

#Create an instance
wn = Toplevel()
wn.title("Rounded Button")

#Create window
wn.geometry("700x300")

#Create button function
def my_func():
   print("Button has been clicked.")

#Import the image
photo = PhotoImage(file=r"C:\Users\jim\Pictures\refresh.png")

img_label= Label(image=photo)

button= Button(wn, image= photo,command= my_func,
borderwidth=0)
button.pack(pady=30)

wn.mainloop()

If the image is large, then, you may face trouble with displaying the image in a button. So, in the above code, we add the image to a label and then display the label over the button. We then set the borderwidth parameter to zero to hide the button border.

Dinux
  • 644
  • 1
  • 6
  • 19