-
I'm developing customtkinter app. In my application when I try to use CtkImages in button I'm getting exception:
My objected-oriented code: class SideBarFrame(ctk.CTkFrame):
def __init__(self, master, **kwargs) -> None:
super().__init__(master, **kwargs)
self.settings_button = ctk.CTkButton(self, hover=False, width=50, text="", image=settings_icon,
command=self.show_settings_frame, fg_color="transparent", border_width=0)
... Function for import os
def load_icon(self, path, path_for_dark: str | None = None) -> ctk.CTkImage:
light_image, dark_image = Image.open(path), None
print(os.path.exists(path))
if path_for_dark:
dark_image = Image.open(path_for_dark)
return ctk.CTkImage(light_image, dark_image, size=(30, 30))
However, in standard code, when I'm write not object-oriented code, code stable working, like this: sidebar = ctk.CTkFrame(app, corner_radius=0)
sidebar.grid(row=0, column=0, sticky="ns")
icon1 = load_icon("assets/settings1-dark.png", "assets/settings1.png")
button1 = ctk.CTkButton(sidebar, hover=False, width=50, text="", image=icon1, , fg_color="transparent", hover_color="lightgray", border_width=0) Same Also I'm tried to use CTkImage straightaway like a image arg: self.settings_button = ctk.CTkButton(self, hover=False, width=50, text="", image=ctk.CTkImage(Image.open("assets/add-dark.png")), command=self.show_settings_frame, fg_color="transparent", border_width=0) It doesn't work. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
@byBenPuls Try the following sample code and tell me if the issue still persists from customtkinter import (
CTk,
CTkButton,
CTkFrame,
CTkImage
)
from PIL.Image import open as open_img
class MyFrame(CTkFrame):
def __init__(self, master: CTk, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self._dict_icon = self.get_icon("dictionary")
self.button = CTkButton(self, text="Dictionary", image=self._dict_icon)
self.button.place(relx=0.5, anchor="center", rely=0.5)
def get_icon(self, what: str) -> CTkImage:
"""Returns icon image based on description of `what`."""
if what == "dictionary":
dark_path = "assets/icon_dictionary_dark.png"
light_path = "assets/icon_dictionary_light.png"
return CTkImage(open_img(light_path), open_img(dark_path), (20, 20))
elif what == ...:
...
class App(CTk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._frame = MyFrame(self)
self._frame.place(relx=0.5, anchor="center", rely=0.5)
if __name__ == "__main__":
app = App()
app.geometry("500x300")
app.mainloop() Assets: Regards |
Beta Was this translation helpful? Give feedback.
-
The problem was solved when I deleted another instance of CTK in the |
Beta Was this translation helpful? Give feedback.
The problem was solved when I deleted another instance of CTK in the
__init__.py
file (i created instance and forgot about it).Thank you, @dipeshSam for your attention!