TomSchimansky / TomSchimansky/CustomTkinter

Error when using tk.PhotoImage on Button

Open
#795 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

information
Dominant language
Python
Stars
13.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

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'

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with customtkinter 5.0.2 and follow the traceback into windows/widgets/ctk_button.py, especially _update_image and the configure path. Check how CTkButton handles the supplied tkinter.PhotoImage, then verify the example no longer raises AttributeError and that the documented image behavior is preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
desktop, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.