From bf1835922b21cbec631fc76577d279f61e3f782b Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Sun, 23 Oct 2022 22:33:57 +0200 Subject: [PATCH] added new button grid system, fixed CTkSwitch grid positioning, moved ctk_canvas.py and draw_engine.py to widgets/core_rendering, --- customtkinter/__init__.py | 11 ++- customtkinter/scaling_tracker.py | 2 +- customtkinter/widgets/__init__.py | 2 +- .../widgets/core_rendering/__init__.py | 0 .../{ => core_rendering}/ctk_canvas.py | 0 .../core_rendering}/draw_engine.py | 2 +- customtkinter/widgets/ctk_button.py | 4 +- customtkinter/widgets/ctk_checkbox.py | 4 +- customtkinter/widgets/ctk_combobox.py | 4 +- customtkinter/widgets/ctk_entry.py | 4 +- customtkinter/widgets/ctk_frame.py | 4 +- customtkinter/widgets/ctk_label.py | 4 +- customtkinter/widgets/ctk_optionmenu.py | 4 +- customtkinter/widgets/ctk_progressbar.py | 4 +- customtkinter/widgets/ctk_radiobutton.py | 4 +- customtkinter/widgets/ctk_scrollbar.py | 4 +- customtkinter/widgets/ctk_slider.py | 4 +- customtkinter/widgets/ctk_switch.py | 8 +- customtkinter/widgets/ctk_tabview.py | 4 +- customtkinter/widgets/ctk_textbox.py | 4 +- customtkinter/widgets/dropdown_menu.py | 2 - customtkinter/windows/ctk_input_dialog.py | 3 - examples/example_button_images.py | 74 ++++++++++++++++++- 23 files changed, 111 insertions(+), 45 deletions(-) create mode 100644 customtkinter/widgets/core_rendering/__init__.py rename customtkinter/widgets/{ => core_rendering}/ctk_canvas.py (100%) rename customtkinter/{ => widgets/core_rendering}/draw_engine.py (99%) diff --git a/customtkinter/__init__.py b/customtkinter/__init__.py index cb4aad0..54785c0 100644 --- a/customtkinter/__init__.py +++ b/customtkinter/__init__.py @@ -2,16 +2,13 @@ __version__ = "4.6.3" import os import sys -from tkinter.constants import * -from tkinter import StringVar, IntVar, DoubleVar, BooleanVar -from tkinter import filedialog # import manager classes from .appearance_mode_tracker import AppearanceModeTracker from .theme_manager import ThemeManager from .scaling_tracker import ScalingTracker from .font_manager import FontManager -from .draw_engine import DrawEngine +from .widgets.core_rendering.draw_engine import DrawEngine AppearanceModeTracker.init_appearance_mode() @@ -45,7 +42,7 @@ if FontManager.load_font(os.path.join(script_directory, "assets", "fonts", "Cust if DrawEngine.preferred_drawing_method == "font_shapes": sys.stderr.write("customtkinter.__init__ warning: " + "Preferred drawing method 'font_shapes' can not be used because the font file could not be loaded.\n" + - "Using 'circle_shapes' instead. The rendering quality will be bad!") + "Using 'circle_shapes' instead. The rendering quality will be bad!\n") DrawEngine.preferred_drawing_method = "circle_shapes" # import widgets @@ -58,7 +55,7 @@ from .widgets.ctk_frame import CTkFrame from .widgets.ctk_progressbar import CTkProgressBar from .widgets.ctk_label import CTkLabel from .widgets.ctk_radiobutton import CTkRadioButton -from .widgets.ctk_canvas import CTkCanvas +from .widgets.core_rendering.ctk_canvas import CTkCanvas from .widgets.ctk_switch import CTkSwitch from .widgets.ctk_optionmenu import CTkOptionMenu from .widgets.ctk_combobox import CTkComboBox @@ -67,6 +64,8 @@ from .widgets.ctk_textbox import CTkTextbox from .widgets.ctk_tabview import CTkTabview from .widgets.ctk_segmented_button import CTkSegmentedButton +from .widgets.ctk_button_new_grid import CTkButtonNewGrid + # import windows from .windows.ctk_tk import CTk from .windows.ctk_toplevel import CTkToplevel diff --git a/customtkinter/scaling_tracker.py b/customtkinter/scaling_tracker.py index 6582637..7ede16f 100644 --- a/customtkinter/scaling_tracker.py +++ b/customtkinter/scaling_tracker.py @@ -1,6 +1,6 @@ import tkinter import sys -from typing import Callable, Dict +from typing import Callable class ScalingTracker: diff --git a/customtkinter/widgets/__init__.py b/customtkinter/widgets/__init__.py index 62acbf5..517b716 100644 --- a/customtkinter/widgets/__init__.py +++ b/customtkinter/widgets/__init__.py @@ -1,3 +1,3 @@ -from .ctk_canvas import CTkCanvas +from customtkinter.widgets.core_rendering.ctk_canvas import CTkCanvas CTkCanvas.init_font_character_mapping() diff --git a/customtkinter/widgets/core_rendering/__init__.py b/customtkinter/widgets/core_rendering/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/customtkinter/widgets/ctk_canvas.py b/customtkinter/widgets/core_rendering/ctk_canvas.py similarity index 100% rename from customtkinter/widgets/ctk_canvas.py rename to customtkinter/widgets/core_rendering/ctk_canvas.py diff --git a/customtkinter/draw_engine.py b/customtkinter/widgets/core_rendering/draw_engine.py similarity index 99% rename from customtkinter/draw_engine.py rename to customtkinter/widgets/core_rendering/draw_engine.py index 0487105..d0cc0eb 100644 --- a/customtkinter/draw_engine.py +++ b/customtkinter/widgets/core_rendering/draw_engine.py @@ -5,7 +5,7 @@ import tkinter from typing import Union, TYPE_CHECKING if TYPE_CHECKING: - from .widgets.ctk_canvas import CTkCanvas + from customtkinter.widgets.core_rendering.ctk_canvas import CTkCanvas class DrawEngine: diff --git a/customtkinter/widgets/ctk_button.py b/customtkinter/widgets/ctk_button.py index 85e2144..e56ebbc 100644 --- a/customtkinter/widgets/ctk_button.py +++ b/customtkinter/widgets/ctk_button.py @@ -2,9 +2,9 @@ import tkinter import sys from typing import Union, Tuple, Callable -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from ..utility.ctk_font import CTkFont diff --git a/customtkinter/widgets/ctk_checkbox.py b/customtkinter/widgets/ctk_checkbox.py index 6c423a7..d4104db 100644 --- a/customtkinter/widgets/ctk_checkbox.py +++ b/customtkinter/widgets/ctk_checkbox.py @@ -2,9 +2,9 @@ import tkinter import sys from typing import Union, Tuple, Callable -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from ..utility.ctk_font import CTkFont diff --git a/customtkinter/widgets/ctk_combobox.py b/customtkinter/widgets/ctk_combobox.py index 1ddcb1b..9bf1a09 100644 --- a/customtkinter/widgets/ctk_combobox.py +++ b/customtkinter/widgets/ctk_combobox.py @@ -3,9 +3,9 @@ import sys from typing import Union, Tuple, Callable, List from .dropdown_menu import DropdownMenu -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from ..utility.ctk_font import CTkFont diff --git a/customtkinter/widgets/ctk_entry.py b/customtkinter/widgets/ctk_entry.py index cb6cc17..f33baac 100644 --- a/customtkinter/widgets/ctk_entry.py +++ b/customtkinter/widgets/ctk_entry.py @@ -1,9 +1,9 @@ import tkinter from typing import Union, Tuple -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from ..utility.ctk_font import CTkFont diff --git a/customtkinter/widgets/ctk_frame.py b/customtkinter/widgets/ctk_frame.py index a452e57..98f47c0 100644 --- a/customtkinter/widgets/ctk_frame.py +++ b/customtkinter/widgets/ctk_frame.py @@ -1,8 +1,8 @@ from typing import Union, Tuple, List -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass diff --git a/customtkinter/widgets/ctk_label.py b/customtkinter/widgets/ctk_label.py index ac5be5a..27e41ac 100644 --- a/customtkinter/widgets/ctk_label.py +++ b/customtkinter/widgets/ctk_label.py @@ -1,9 +1,9 @@ import tkinter from typing import Union, Tuple, Callable -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from ..utility.ctk_font import CTkFont diff --git a/customtkinter/widgets/ctk_optionmenu.py b/customtkinter/widgets/ctk_optionmenu.py index 187a0b6..26e0bc8 100644 --- a/customtkinter/widgets/ctk_optionmenu.py +++ b/customtkinter/widgets/ctk_optionmenu.py @@ -2,9 +2,9 @@ import tkinter import sys from typing import Union, Tuple, Callable -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from .dropdown_menu import DropdownMenu from ..utility.ctk_font import CTkFont diff --git a/customtkinter/widgets/ctk_progressbar.py b/customtkinter/widgets/ctk_progressbar.py index 783e360..899060a 100644 --- a/customtkinter/widgets/ctk_progressbar.py +++ b/customtkinter/widgets/ctk_progressbar.py @@ -2,9 +2,9 @@ import tkinter import math from typing import Union, Tuple -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass diff --git a/customtkinter/widgets/ctk_radiobutton.py b/customtkinter/widgets/ctk_radiobutton.py index 683a0e9..d32f95c 100644 --- a/customtkinter/widgets/ctk_radiobutton.py +++ b/customtkinter/widgets/ctk_radiobutton.py @@ -2,9 +2,9 @@ import tkinter import sys from typing import Union, Tuple, Callable -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from ..utility.ctk_font import CTkFont diff --git a/customtkinter/widgets/ctk_scrollbar.py b/customtkinter/widgets/ctk_scrollbar.py index e96cf0d..dbe17ea 100644 --- a/customtkinter/widgets/ctk_scrollbar.py +++ b/customtkinter/widgets/ctk_scrollbar.py @@ -1,9 +1,9 @@ import sys from typing import Union, Tuple, Callable -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass diff --git a/customtkinter/widgets/ctk_slider.py b/customtkinter/widgets/ctk_slider.py index aed5a99..bf5b28e 100644 --- a/customtkinter/widgets/ctk_slider.py +++ b/customtkinter/widgets/ctk_slider.py @@ -2,9 +2,9 @@ import tkinter import sys from typing import Union, Tuple, Callable -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass diff --git a/customtkinter/widgets/ctk_switch.py b/customtkinter/widgets/ctk_switch.py index db9a17c..e6a1a65 100644 --- a/customtkinter/widgets/ctk_switch.py +++ b/customtkinter/widgets/ctk_switch.py @@ -2,9 +2,9 @@ import tkinter import sys from typing import Union, Tuple, Callable -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from ..utility.ctk_font import CTkFont @@ -90,9 +90,9 @@ class CTkSwitch(CTkBaseClass): self._textvariable = textvariable # configure grid system (3x1) - self.grid_columnconfigure(0, weight=1) + self.grid_columnconfigure(0, weight=0) self.grid_columnconfigure(1, weight=0, minsize=self._apply_widget_scaling(6)) - self.grid_columnconfigure(2, weight=0) + self.grid_columnconfigure(2, weight=1) self._bg_canvas = CTkCanvas(master=self, highlightthickness=0, diff --git a/customtkinter/widgets/ctk_tabview.py b/customtkinter/widgets/ctk_tabview.py index b1f8632..c1490fd 100644 --- a/customtkinter/widgets/ctk_tabview.py +++ b/customtkinter/widgets/ctk_tabview.py @@ -4,8 +4,8 @@ from ..theme_manager import ThemeManager from .ctk_frame import CTkFrame from .widget_base_class import CTkBaseClass from .ctk_segmented_button import CTkSegmentedButton -from .ctk_canvas import CTkCanvas -from ..draw_engine import DrawEngine +from .core_rendering.ctk_canvas import CTkCanvas +from .core_rendering.draw_engine import DrawEngine class CTkTabview(CTkBaseClass): diff --git a/customtkinter/widgets/ctk_textbox.py b/customtkinter/widgets/ctk_textbox.py index 64a12f1..8d9ad1c 100644 --- a/customtkinter/widgets/ctk_textbox.py +++ b/customtkinter/widgets/ctk_textbox.py @@ -1,10 +1,10 @@ import tkinter from typing import Union, Tuple -from .ctk_canvas import CTkCanvas +from .core_rendering.ctk_canvas import CTkCanvas from .ctk_scrollbar import CTkScrollbar from ..theme_manager import ThemeManager -from ..draw_engine import DrawEngine +from .core_rendering.draw_engine import DrawEngine from .widget_base_class import CTkBaseClass from ..utility.ctk_font import CTkFont diff --git a/customtkinter/widgets/dropdown_menu.py b/customtkinter/widgets/dropdown_menu.py index db50349..92ff48b 100644 --- a/customtkinter/widgets/dropdown_menu.py +++ b/customtkinter/widgets/dropdown_menu.py @@ -1,7 +1,5 @@ import tkinter import sys -import copy -import re from typing import Union, Tuple, Callable, List from ..theme_manager import ThemeManager diff --git a/customtkinter/windows/ctk_input_dialog.py b/customtkinter/windows/ctk_input_dialog.py index 3a2a73e..2f1e72c 100644 --- a/customtkinter/windows/ctk_input_dialog.py +++ b/customtkinter/windows/ctk_input_dialog.py @@ -1,10 +1,7 @@ -import tkinter -import time from typing import Union, Tuple from ..widgets.ctk_label import CTkLabel from ..widgets.ctk_entry import CTkEntry -from ..widgets.ctk_frame import CTkFrame from ..windows.ctk_toplevel import CTkToplevel from ..widgets.ctk_button import CTkButton from ..appearance_mode_tracker import AppearanceModeTracker diff --git a/examples/example_button_images.py b/examples/example_button_images.py index 62c4300..23c9374 100644 --- a/examples/example_button_images.py +++ b/examples/example_button_images.py @@ -1,3 +1,5 @@ +import PIL.ImageTk + import customtkinter import tkinter from PIL import Image, ImageTk @@ -31,7 +33,77 @@ class App(customtkinter.CTk): self.chat_image = self.load_image("/test_images/chat.png", 20) self.home_image = self.load_image("/test_images/home.png", 20) - self.button_1 = customtkinter.CTkButton(master=self.frame_1, image=self.add_folder_image, text="Add Folder", height=32, + def _pyimagingtkcall(command, photo, id): + tk = photo.tk + try: + tk.call(command, photo, id) + except tkinter.TclError: + print("_pyimagingtkcall error") + + class PhotoImage: + def __init__(self, image=None, size=None, **kw): + if hasattr(image, "mode") and hasattr(image, "size"): + # got an image instead of a mode + mode = image.mode + if mode == "P": + # palette mapped data + image.apply_transparency() + image.load() + try: + mode = image.palette.mode + except AttributeError: + mode = "RGB" # default + size = image.size + kw["width"], kw["height"] = size + else: + mode = image + image = None + + if mode not in ["1", "L", "RGB", "RGBA"]: + mode = Image.getmodebase(mode) + + self.__mode = mode + self.__size = size + self.__photo = tkinter.PhotoImage(**kw) + self.tk = self.__photo.tk + if image: + self.paste(image) + + def __del__(self): + name = self.__photo.name + self.__photo.name = None + try: + self.__photo.tk.call("image", "delete", name) + except Exception: + pass # ignore internal errors + + def __str__(self): + return str(self.__photo) + + def width(self): + return self.__size[0] + + def height(self): + return self.__size[1] + + def paste(self, im, box=None): + if box is not None: + deprecate("The box parameter", 10, None) + + # convert to blittable + im.load() + image = im.im + if image.isblock() and im.mode == self.__mode: + block = image + else: + block = image.new_block(self.__mode, im.size) + image.convert2(block, image) # convert directly between buffers + _pyimagingtkcall("PyImagingPhoto", self.__photo, block.id) + + pil_img = Image.open(PATH + "/test_images/add-folder.png").resize((10, 10)) + image = PhotoImage(pil_img) + + self.button_1 = customtkinter.CTkButton(master=self.frame_1, image=image, text="Add Folder", height=32, compound="right", command=self.button_function) self.button_1.grid(row=1, column=0, columnspan=2, padx=20, pady=(20, 10), sticky="ew")