TomSchimansky / CustomTkinter

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

iconphoto is not working #2387

Closed adamtcdev closed 2 months ago

adamtcdev commented 2 months ago

code:

class root(CTk):
    def __init__(self):
        super().__init__()
        set_widget_scaling(1.2)

        self.title("TinEdu")
        self.geometry('1200x720')
        self.iconphoto(1, PhotoImage('.\\icons\\tiny_logo.png'))

output:

Traceback (most recent call last):
  File "g:\My Drive\TinEdu\v0.2-0304-beta\main.py", line 157, in <module>
    root = root()
           ^^^^^^
  File "g:\My Drive\TinEdu\v0.2-0304-beta\main.py", line 16, in __init__
    self.iconphoto(1, PhotoImage('.\\icons\\tiny_logo.png'))
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2200, in wm_iconphoto
    self.tk.call('wm', 'iconphoto', self._w, "-default", *args)
_tkinter.TclError: failed to create an iconphoto with image ".\icons\tiny_logo.png"
Akascape commented 2 months ago

@adamtcdev Try this method:

For root window:

import os
from PIL import ImageTk
...

self.iconpath = ImageTk.PhotoImage(file='logo.png')
self.wm_iconbitmap()
self.iconphoto(False, self.iconpath)

For toplevels:

self.wm_iconbitmap()
self.after(300, lambda: self.iconphoto(False, self.iconpath))
adamtcdev commented 2 months ago

Thanks.

adamtcdev commented 2 months ago

I am gonna close this issue.