TomSchimansky / CustomTkinter

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

Error when using tk.PhotoImage on Button #795

Open alejandroautalan opened 1 year ago

alejandroautalan commented 1 year ago

Hello, I have an error when using tk.PhotoImage on Button.

customtkinter version: 5.0.2

Example:

#!/usr/bin/python3
import tkinter as tk
from customtkinter import (CTk, CTkButton, CTkFrame)

class CtkButtonImageApp:
    def __init__(self, master=None):
        # build ui
        ctk1 = CTk(master)
        ctk1.geometry("320x200")
        ctkframe1 = CTkFrame(ctk1)
        ctkbutton1 = CTkButton(ctkframe1)
        self.img_135search = tk.PhotoImage(file="135-search.png")
        ctkbutton1.configure(image=self.img_135search, text='Search')
        ctkbutton1.pack(side="top")
        ctkframe1.pack(expand="true", side="top")

        # Main widget
        self.mainwindow = ctk1

    def run(self):
        self.mainwindow.mainloop()

if __name__ == "__main__":
    app = CtkButtonImageApp()
    app.run()

Error:

AttributeError: 'PhotoImage' object has no attribute 'create_scaled_photo_image'

Trace:

fades -r reqdev-ctk-v5.txt ctk_button_image_app.py 
CTkButton Warning: Given image is not CTkImage but <class 'tkinter.PhotoImage'>. Image can not be scaled on HighDPI displays, use CTkImage instead.
Traceback (most recent call last):
  File "/home/alejandro/Privado/projects/pygubu-project/issues/designer/ctk_button_image_app.py", line 26, in <module>
    app = CtkButtonImageApp()
  File "/home/alejandro/Privado/projects/pygubu-project/issues/designer/ctk_button_image_app.py", line 14, in __init__
    ctkbutton1.configure(image=self.img_135search, text='Search')
  File "/home/alejandro/.local/share/fades/43aaa717-a9e8-4062-a3da-40e427b75a69/lib/python3.10/site-packages/customtkinter/windows/widgets/ctk_button.py", line 419, in configure
    super().configure(require_redraw=require_redraw, **kwargs)
  File "/home/alejandro/.local/share/fades/43aaa717-a9e8-4062-a3da-40e427b75a69/lib/python3.10/site-packages/customtkinter/windows/widgets/core_widget_classes/ctk_base_class.py", line 138, in configure
    self._draw()
  File "/home/alejandro/.local/share/fades/43aaa717-a9e8-4062-a3da-40e427b75a69/lib/python3.10/site-packages/customtkinter/windows/widgets/ctk_button.py", line 243, in _draw
    self._update_image()  # set image
  File "/home/alejandro/.local/share/fades/43aaa717-a9e8-4062-a3da-40e427b75a69/lib/python3.10/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'
TomSchimansky commented 1 year ago

Only PIL PhotoImage and CTkImage are supported at the moment, but the preferred way would be to use the CTkImage as it supports scaling an light/dark mode.

alejandroautalan commented 1 year ago

Hello, thanks for the info.

Then, the type annotation should be fixed.

https://github.com/TomSchimansky/CustomTkinter/blob/77595da9f2254f891a74fb66492943a380214b0a/customtkinter/windows/widgets/ctk_button.py#L43

Regards

TomSchimansky commented 1 year ago

Yes I will change this.

h43lb1t0 commented 1 year ago

I still get the error File "/media/haelbito/Server1/Dokumente/Code/eigene_Projekte/Python/PDFmerger/merger/lib/python3.8/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'

My Code:

import customtkinter
from PIL import Image, ImageTk
def createPhotoImage(self, task: str) :
        return ImageTk.PhotoImage(file=self.getImagePath(task))

button = customtkinter.CTkButton(master=master,
                                    width=120,
                                    height=32,
                                    border_width=0,
                                    corner_radius=8,
                                    text="CTkButton",
                                    command=command,
                                    image=createPhotoImage("foo")
                                    )
DanyYnad commented 1 year ago

Hello, the problem comes from customtkinter version. For 4.5.X and 4.6.X it should work. For the remains versions it doesn't work. So use a version working properly (4.5.X and 4.6.X).

DanyYnad commented 1 year ago

Hello, I have an error when using tk.PhotoImage on Button.

customtkinter version: 5.0.2

Example:

#!/usr/bin/python3
import tkinter as tk
from customtkinter import (CTk, CTkButton, CTkFrame)

class CtkButtonImageApp:
    def __init__(self, master=None):
        # build ui
        ctk1 = CTk(master)
        ctk1.geometry("320x200")
        ctkframe1 = CTkFrame(ctk1)
        ctkbutton1 = CTkButton(ctkframe1)
        self.img_135search = tk.PhotoImage(file="135-search.png")
        ctkbutton1.configure(image=self.img_135search, text='Search')
        ctkbutton1.pack(side="top")
        ctkframe1.pack(expand="true", side="top")

        # Main widget
        self.mainwindow = ctk1

    def run(self):
        self.mainwindow.mainloop()

if __name__ == "__main__":
    app = CtkButtonImageApp()
    app.run()

Error:

AttributeError: 'PhotoImage' object has no attribute 'create_scaled_photo_image'

Trace:

fades -r reqdev-ctk-v5.txt ctk_button_image_app.py 
CTkButton Warning: Given image is not CTkImage but <class 'tkinter.PhotoImage'>. Image can not be scaled on HighDPI displays, use CTkImage instead.
Traceback (most recent call last):
  File "/home/alejandro/Privado/projects/pygubu-project/issues/designer/ctk_button_image_app.py", line 26, in <module>
    app = CtkButtonImageApp()
  File "/home/alejandro/Privado/projects/pygubu-project/issues/designer/ctk_button_image_app.py", line 14, in __init__
    ctkbutton1.configure(image=self.img_135search, text='Search')
  File "/home/alejandro/.local/share/fades/43aaa717-a9e8-4062-a3da-40e427b75a69/lib/python3.10/site-packages/customtkinter/windows/widgets/ctk_button.py", line 419, in configure
    super().configure(require_redraw=require_redraw, **kwargs)
  File "/home/alejandro/.local/share/fades/43aaa717-a9e8-4062-a3da-40e427b75a69/lib/python3.10/site-packages/customtkinter/windows/widgets/core_widget_classes/ctk_base_class.py", line 138, in configure
    self._draw()
  File "/home/alejandro/.local/share/fades/43aaa717-a9e8-4062-a3da-40e427b75a69/lib/python3.10/site-packages/customtkinter/windows/widgets/ctk_button.py", line 243, in _draw
    self._update_image()  # set image
  File "/home/alejandro/.local/share/fades/43aaa717-a9e8-4062-a3da-40e427b75a69/lib/python3.10/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'

Hello, the problem comes from customtkinter version. For 4.5.X and 4.6.X it should work. For the remains versions it doesn't work. So use a version working properly (4.5.X and 4.6.X).