TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
10.77k stars 1.03k forks source link

How do I add an image to a button? #2422

Open Beatriz910 opened 1 month ago

Beatriz910 commented 1 month ago

i know ive read the documentation but im a beginner but i tried but it doesnt work

`import customtkinter as ctk from PIL import Image, ImageTk

app = ctk.CTk()

image = Image.open(save.png) photo = ImageTk.PhotoImage(image)

button = ctk.CTkButton(app, image=photo, text="") # Empty text to show only the image button.pack(pady=20, padx=20)

app.mainloop() `

AnonymousVibrate commented 1 month ago

Make sure the image path is correct.

image_path = './ImageFolder/save.png'
image = Image.open(image_path)
Betrayal5414 commented 4 weeks ago

i know ive read the documentation but im a beginner but i tried but it doesnt work

`import customtkinter as ctk from PIL import Image, ImageTk

app = ctk.CTk()

image = Image.open(save.png) photo = ImageTk.PhotoImage(image)

button = ctk.CTkButton(app, image=photo, text="") # Empty text to show only the image button.pack(pady=20, padx=20)

app.mainloop() `

Try it

from PIL import Image

My_image = ctk.CTkImage(light_image=Image.open("image.png"), size=(10, 10))

Button = ctk.CTkButton(app, image=My_image, text="")
Button.pack()

Don't forget the "" in the open() function

you're off to a good start but you forgot the "" here -> image = Image.open(save.png)