CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

CTkImage ImportError: PIL.Image and PIL.ImageTk couldn't be imported

Open James46113 opened this issue 3 years ago • 3 comments

I'm trying to put an image on a label, and saw this warning:

CTkLabel Warning: Given image is not CTkImage but <class 'tkinter.PhotoImage'>. Image can not be scaled on HighDPI displays, use CTkImage instead.

So I switched to a CTkImage but now I'm getting this error:

Traceback (most recent call last):
  File "/home/james/file_explorer/.env/lib/python3.10/site-packages/customtkinter/windows/widgets/image/ctk_image.py", line 41, in _check_pil_import
    _, _ = Image, ImageTk
NameError: name 'Image' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/james/file_explorer/gui_main.py", line 40, in <module>
    f1 = File1("test1", tk, 0)
  File "/home/james/file_explorer/gui_main.py", line 10, in __init__
    self.icon = CTkImage("file.png").subsample(10)
  File "/home/james/file_explorer/.env/lib/python3.10/site-packages/customtkinter/windows/widgets/image/ctk_image.py", line 27, in __init__
    self._check_pil_import()
  File "/home/james/file_explorer/.env/lib/python3.10/site-packages/customtkinter/windows/widgets/image/ctk_image.py", line 43, in _check_pil_import
    raise ImportError("PIL.Image and PIL.ImageTk couldn't be imported")
ImportError: PIL.Image and PIL.ImageTk couldn't be imported

Everything worked file using tkinter's PhotoImage, and I have PIL installed.

James46113 avatar Dec 04 '22 12:12 James46113

Try this format: my_image = customtkinter.CTkImage(light_image=Image.open("<path to light mode image>"), dark_image=Image.open("<path to dark mode image>"), size=(30, 30))

duruburak avatar Dec 04 '22 15:12 duruburak

It doesn't throw an error, but it does stretch the image which I can't seem to do anything about.

James46113 avatar Dec 04 '22 15:12 James46113

Yeah I didn't see the new patch update my bad, I updated the code now it should be working fine. Try it.

duruburak avatar Dec 04 '22 15:12 duruburak

from PIL import Image, ImageTk has to work, then everything should be fine.

TomSchimansky avatar Dec 04 '22 23:12 TomSchimansky