Home > OS >  How i can solve this problem in Python code (TKinter, Pillow, customtkinter)
How i can solve this problem in Python code (TKinter, Pillow, customtkinter)

Time:01-10

I'm trying to add an image to Customtkinter Buttom, but an error occurs

import sqlite3
from tkinter import \*
import customtkinter
from tkinter import ttk
from PIL import Image, ImageTk

customtkinter.set_appearance_mode('dark')
customtkinter.set_default_color_theme('blue')

\#Create a Ctk instance(app)
main = customtkinter.CTk()
main.geometry("400x240")
main.resizable(width=False, height=False)

\#Username Frame
username_tittle = customtkinter.CTkLabel(master=main, text='Username:').place(relx=0.2, rely= 0.3)
username_box = customtkinter.CTkEntry(master=main, width=150)
username_box.place(relx=0.43, rely=0.3)
python_image = ImageTk.PhotoImage(Image.open('user_icon.png'), Image.ANTIALIAS, )
but = customtkinter.CTkButton(master=main, image=python_image).pack()

this code generate the following error:

c:\\Users\\claud\\OneDrive\\Documentos\\meuusprojetos\\Login\\main.py:19: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use LANCZOS or Resampling.LANCZOS instead.
python_image = ImageTk.PhotoImage(Image.open('user_icon.png'), Image.ANTIALIAS, )
CTkButton Warning: Given image is not CTkImage but \<class 'PIL.ImageTk.PhotoImage'\>. Image can not be scaled on HighDPI displays, use CTkImage instead.
Traceback (most recent call last):
File "c:\\Users\\claud\\OneDrive\\Documentos\\meuusprojetos\\Login\\main.py", line 20, in \<module\>
but = customtkinter.CTkButton(master=main, image=python_image).pack()
File "C:\\Users\\claud\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\customtkinter\\windows\\widgets\\ctk_button.py", line 106, in __init__
self.\_draw()
File "C:\\Users\\claud\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\customtkinter\\windows\\widgets\\ctk_button.py", line 243, in \_draw
self.\_update_image()  # set image
File "C:\\Users\\claud\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\customtkinter\\windows\\widgets\\ctk_button.py", line 154, in \_update_image
self.\_image_label.configure(image=self.\_image.create_scaled_photo_image(self.\_get_widget_scaling(),
AttributeError: 'PhotoImage' object has no attribute 'create_scaled_photo_image'

Can anybody help me?

CodePudding user response:

It looks like you are encountering an error when trying to add an image to a CTkButton widget in the customtkinter module.

The error message mentions that the given image is not a CTkImage, but a PIL.ImageTk.PhotoImage.

To fix this issue, you can try the following:

Use the CTkImage class to create an image object instead of the PIL.ImageTk.PhotoImage class. You can do this by using the CTkImage.open() method to open the image file, like this:

python_image = customtkinter.CTkImage.open('user_icon.png')

I hope this suggestion will help you.

CodePudding user response:

Latest version of customtkinter accepts CTkImage only for image option of its widgets:

...
python_image = customtkinter.CTkImage(Image.open("user_icon.png"))
customtkinter.CTkButton(master=main, image=python_image).pack()
...
  • Related