-1

I am trying to add a small icon next to one of my buttons for my app, however when I import the image and place it in the window it is just a grey box. The image I am adding is not transparent and in a jpg format, I have tried a png format also, ideally I would want it to accept a transparent png. My window has 2 frames, one for the side bar and one for the main screen. I am using customtkinter library, which makes tkinter look better (https://github.com/TomSchimansky/CustomTkinter). The image is in the same directory as the py file. The following is my code and images of the GUI before and after adding the code for the image.

code:

home_button = customtkinter.CTkButton(left_frame, text="Home", width=195, height=40, 
                                  corner_radius=10,
                                  text_font=["", "16"],
                                  fg_color=["#ffffff", "#000000"],
                                  hover_color=["#f4f5fa", "#1d1e20"],
                                  command=home
                                  )
home_button.place(x=50, y=80)
home_button.text_label.grid(sticky="w")

home_ico = ImageTk.PhotoImage(Image.open("home_def.jpg"))
home_label = Label(image=home_ico)
home_label.place(x=20, y=80)

GUI before adding image:

GUI after adding image:

  • Your image source is most likely incorrect, it is either in another folder or you may need to add a `/` before the source path, unless its in a different folder where you will need to do `..//"home_def.jpg"`, which will take you one folder backwards so you can go to another child folder of the parent directory – tygzy Nov 16 '22 at 14:40
  • Does this code exist in a function? – Bryan Oakley Nov 16 '22 at 15:38
  • @tygzy: if the path to the mage file was wrong, tkinter would throw an error. – Bryan Oakley Nov 16 '22 at 15:38
  • @BryanOakley Yes it does, when I took it out of the function the image displays correctly – volcaniceruption09845 Nov 17 '22 at 03:40

1 Answers1

-1

You are probably making a bad reference to the path of the image file. I bet this image is on the same folder of the .py file but that's not how python works.

You will need to get OS. PATH and make a reference to your source folder.