created image example

This commit is contained in:
Tom Schimansky 2022-11-30 21:31:40 +01:00
parent f0bbc188a2
commit ae0c5a3397
9 changed files with 112 additions and 5 deletions

View File

@ -118,13 +118,16 @@ class CTkButton(CTkBaseClass):
if self._text_label is not None:
self._text_label.configure(font=self._apply_font_scaling(self._font))
if self._image_label is not None:
self._update_image()
self._update_image()
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
height=self._apply_widget_scaling(self._desired_height))
self._draw(no_color_updates=True)
def _set_appearance_mode(self, mode_string):
super()._set_appearance_mode(mode_string)
self._update_image()
def _set_dimensions(self, width: int = None, height: int = None):
super()._set_dimensions(width, height)

View File

@ -101,8 +101,13 @@ class CTkLabel(CTkBaseClass):
self._label.configure(wraplength=self._apply_widget_scaling(self._wraplength))
self._create_grid()
self._update_image()
self._draw(no_color_updates=True)
def _set_appearance_mode(self, mode_string):
super()._set_appearance_mode(mode_string)
self._update_image()
def _set_dimensions(self, width=None, height=None):
super()._set_dimensions(width, height)
@ -165,9 +170,6 @@ class CTkLabel(CTkBaseClass):
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
if self._image is not None:
self._update_image()
def configure(self, require_redraw=False, **kwargs):
if "corner_radius" in kwargs:
self._corner_radius = kwargs.pop("corner_radius")

102
examples/image_example.py Normal file
View File

@ -0,0 +1,102 @@
import customtkinter
import os
from PIL import Image
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.title("image_example.py")
self.geometry("700x450")
# set grid layout 1x2
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
# load images with light and dark mode image
current_path = os.path.dirname(os.path.realpath(__file__))
self.home_image = customtkinter.CTkImage(light_image=Image.open(current_path + "/test_images/home_dark.png"),
dark_image=Image.open(current_path + "/test_images/home_light.png"), size=(20, 20))
self.chat_image = customtkinter.CTkImage(light_image=Image.open(current_path + "/test_images/chat_dark.png"),
dark_image=Image.open(current_path + "/test_images/chat_light.png"), size=(20, 20))
self.add_user_image = customtkinter.CTkImage(light_image=Image.open(current_path + "/test_images/add_user_dark.png"),
dark_image=Image.open(current_path + "/test_images/add_user_light.png"), size=(20, 20))
# create navigation frame
self.navigation_frame = customtkinter.CTkFrame(self, corner_radius=0)
self.navigation_frame.grid(row=0, column=0, sticky="nsew")
self.navigation_frame.grid_rowconfigure(4, weight=1)
self.navigation_frame_label = customtkinter.CTkLabel(self.navigation_frame, text="Image Example",
font=customtkinter.CTkFont(size=18, weight="bold"))
self.navigation_frame_label.grid(row=0, column=0, padx=20, pady=20)
self.home_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Home",
fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
image=self.home_image, anchor="w", command=self.home_button_event)
self.home_button.grid(row=1, column=0, sticky="ew")
self.chat_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="New Chat",
fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
image=self.chat_image, anchor="w", command=self.chat_button_event)
self.chat_button.grid(row=2, column=0, sticky="ew")
self.add_user_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Add User",
fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
image=self.add_user_image, anchor="w", command=self.add_user_button_event)
self.add_user_button.grid(row=3, column=0, sticky="ew")
self.appearance_mode_menu = customtkinter.CTkOptionMenu(self.navigation_frame, values=["Light", "Dark", "System"],
command=self.change_appearance_mode_event)
self.appearance_mode_menu.grid(row=6, column=0, padx=20, pady=20, sticky="s")
# create home frame
self.home_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
self.home_frame.grid(row=0, column=1, sticky="nsew")
# create chat frame
self.chat_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
self.chat_frame.grid(row=0, column=1, sticky="nsew")
# create add user frame
self.add_user_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
self.add_user_frame.grid(row=0, column=1, sticky="nsew")
def select_frame_by_name(self, name):
# set button color for selected button
self.home_button.configure(fg_color=("gray75", "gray25") if name == "home" else "transparent")
self.chat_button.configure(fg_color=("gray75", "gray25") if name == "chat" else "transparent")
self.add_user_button.configure(fg_color=("gray75", "gray25") if name == "add_user" else "transparent")
# show selected frame
if name == "home":
self.home_frame.grid(row=0, column=1, sticky="nsew")
else:
self.home_frame.grid_forget()
if name == "home":
self.home_frame.grid(row=0, column=1, sticky="nsew")
else:
self.home_frame.grid_forget()
if name == "home":
self.home_frame.grid(row=0, column=1, sticky="nsew")
else:
self.home_frame.grid_forget()
self.chat_button.configure(fg_color="transparent")
self.add_user_button.configure(fg_color="transparent")
def home_button_event(self):
self.select_frame_by_name("home")
def chat_button_event(self):
self.select_frame_by_name("chat")
def add_user_button_event(self):
self.select_frame_by_name("add_user")
def change_appearance_mode_event(self, new_appearance_mode):
customtkinter.set_appearance_mode(new_appearance_mode)
if __name__ == "__main__":
app = App()
app.mainloop()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB