From af73c9ad5f1e16773fe9c1e78840ec65fc323ca5 Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Sat, 1 Jan 2022 22:16:06 +0100 Subject: [PATCH] readme update for CTk class and bug fix for .destroy() for all widgets --- Readme.md | 55 +++++++++++++++++----- customtkinter/__init__.py | 2 +- customtkinter/appearance_mode_tracker.py | 17 ++++++- customtkinter/customtkinter_button.py | 4 ++ customtkinter/customtkinter_checkbox.py | 4 ++ customtkinter/customtkinter_entry.py | 4 ++ customtkinter/customtkinter_frame.py | 4 ++ customtkinter/customtkinter_label.py | 4 ++ customtkinter/customtkinter_progressbar.py | 4 ++ customtkinter/customtkinter_slider.py | 4 ++ customtkinter/customtkinter_tk.py | 3 +- setup.py | 2 +- 12 files changed, 90 insertions(+), 17 deletions(-) diff --git a/Readme.md b/Readme.md index 83e8cb5..aa8f4c7 100644 --- a/Readme.md +++ b/Readme.md @@ -35,7 +35,7 @@ To test customtkinter you can try this simple example with only a single button: import tkinter import customtkinter # <- import the CustomTkinter module -root_tk = tkinter.Tk() # create the Tk window like you normally do +root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window) root_tk.geometry("400x240") root_tk.title("CustomTkinter Test") @@ -69,23 +69,25 @@ button = customtkinter.CTkButton(master=root_tk, button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER) ``` -### How to use macOS dark mode? -If you have a python version with Tcl/Tk >= 8.6.9, then you can enable the macOS -darkmode. Currently only the anaconda python versions have Tcl/Tk >= 8.6.9. -So if you want a dark window titlebar, you have to install anaconda python version +### Dark mode and dark title-bar on macOS +If you have a python version with Tcl/Tk >= 8.6.9, then you automatically get +a dark title bar with macOS dark-mode on, if you use the `customtkinter.Ctk` class to create +the window instead of the normal `tkinterTk` class. Currently, only the anaconda python versions have Tcl/Tk >= 8.6.9. +So if you want a dark window title-bar, you have to install anaconda python version or miniconda. ```python import tkinter import customtkinter -customtkinter.enable_macos_darkmode() customtkinter.set_appearance_mode("System") +root_tk = customtkinter.CTk() ... the program ... -customtkinter.disable_macos_darkmode() +root_tk.mainloop() + ``` -which gives the following with the above simple button program: +The above results in a window with a black title-bar with macOS dark-mode turned on: ![](documentation_images/simple_macOS_darkmode_test.png) @@ -96,14 +98,14 @@ the System mode: ### Advanced example with multiple CTkFrames -Here I used the ``customtkinter.enable_macos_darkmode()`` command to -enable the macOS darkmode, and used multpiple CTkFrames. It has some +Here I used the ``customtkinter.CTk()`` class to create the main window with two CTkFrame's and +set the appearance mode to `System`. It has some kind of a menu on the left side, and I used all CustomTkinter elements there are at the moment.Maybe this is a good reference if you want to create your own application with this library. (Code: /complex_example.py) -With macOS darkmode turned on, it looks like this: +With macOS dark-mode turned on, it looks like this: ![](documentation_images/complex_example_dark.png) @@ -147,6 +149,37 @@ created two buttons with a bell and a settings image on them: ## Documentation - CustomTkinter Elements +### CTk +You can use the normal ``kinter.Tk`` class to create the root window, +but if you want a background color that changes with the appearance mode and a dark title-bar on macOS, +you should use the `customtkinter.CTk` class which behaves exactly like the normal Tk +class, except that you can also set a tuple color as bg color. + +Example Code: + +```python +root_tk = customtkinter.CTk() + +... program ... + +root_tk.mainloop() +``` +
+Show all arguments: + +argument | value +--- | --- +bg_color or bg | tuple: (light_color, dark_color) or single color + +CTk Methods: + +```python +root_tk = customtkinter.CTk() +# configure bg color with single or tuple color +root_tk.configure(bg_color="gray20") +root_tk.configure(bg_color=(, )) +``` + ### CTkButton Examle Code: ```python diff --git a/customtkinter/__init__.py b/customtkinter/__init__.py index 83afb93..fe92d2b 100644 --- a/customtkinter/__init__.py +++ b/customtkinter/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.5" +__version__ = "1.6" from .customtkinter_button import CTkButton from .customtkinter_slider import CTkSlider diff --git a/customtkinter/appearance_mode_tracker.py b/customtkinter/appearance_mode_tracker.py index 8dd1872..39481f8 100644 --- a/customtkinter/appearance_mode_tracker.py +++ b/customtkinter/appearance_mode_tracker.py @@ -119,6 +119,10 @@ class AppearanceModeTracker(): def add(cls, callback): cls.callback_list.append(callback) + @classmethod + def remove(cls, callback): + cls.callback_list.remove(callback) + @classmethod def get_mode(cls): return cls.appearance_mode @@ -141,10 +145,19 @@ class AppearanceModeTracker(): if cls.appearance_mode == 0: for callback in cls.callback_list: - callback("Light") + try: + callback("Light") + except Exception: + print("error callback") + continue + elif cls.appearance_mode == 1: for callback in cls.callback_list: - callback("Dark") + try: + callback("Dark") + except Exception: + print("error callback") + continue AppearanceModeTracker.init_listener_function() diff --git a/customtkinter/customtkinter_button.py b/customtkinter/customtkinter_button.py index 4610ebc..eaa47d0 100644 --- a/customtkinter/customtkinter_button.py +++ b/customtkinter/customtkinter_button.py @@ -101,6 +101,10 @@ class CTkButton(tkinter.Frame): self.bind('', self.update_dimensions) self.draw() + def destroy(self): + AppearanceModeTracker.remove(self.set_appearance_mode) + super().destroy() + def configure_basic_grid(self): # Configuration of a basic grid (2x2) in which all elements of CTkButtons are centered on one row and one column self.grid_rowconfigure(0, weight=1) diff --git a/customtkinter/customtkinter_checkbox.py b/customtkinter/customtkinter_checkbox.py index 74f1b41..08b8e11 100644 --- a/customtkinter/customtkinter_checkbox.py +++ b/customtkinter/customtkinter_checkbox.py @@ -91,6 +91,10 @@ class CTkCheckBox(tkinter.Frame): self.draw() + def destroy(self): + AppearanceModeTracker.remove(self.set_appearance_mode) + super().destroy() + def detect_color_of_master(self): if isinstance(self.master, CTkFrame): return self.master.fg_color diff --git a/customtkinter/customtkinter_entry.py b/customtkinter/customtkinter_entry.py index 9acf6e7..af2009f 100644 --- a/customtkinter/customtkinter_entry.py +++ b/customtkinter/customtkinter_entry.py @@ -54,6 +54,10 @@ class CTkEntry(tkinter.Frame): self.draw() + def destroy(self): + AppearanceModeTracker.remove(self.change_appearance_mode) + super().destroy() + def detect_color_of_master(self): if isinstance(self.master, CTkFrame): return self.master.fg_color diff --git a/customtkinter/customtkinter_frame.py b/customtkinter/customtkinter_frame.py index 6874aee..403e3ba 100644 --- a/customtkinter/customtkinter_frame.py +++ b/customtkinter/customtkinter_frame.py @@ -58,6 +58,10 @@ class CTkFrame(tkinter.Frame): self.draw() + def destroy(self): + AppearanceModeTracker.remove(self.change_appearance_mode) + super().destroy() + def detect_color_of_master(self): if isinstance(self.master, CTkFrame): return self.master.fg_color diff --git a/customtkinter/customtkinter_label.py b/customtkinter/customtkinter_label.py index fa54bc6..55f92ac 100644 --- a/customtkinter/customtkinter_label.py +++ b/customtkinter/customtkinter_label.py @@ -69,6 +69,10 @@ class CTkLabel(tkinter.Frame): self.draw() + def destroy(self): + AppearanceModeTracker.remove(self.change_appearance_mode) + super().destroy() + def detect_color_of_master(self): if isinstance(self.master, CTkFrame): return self.master.fg_color diff --git a/customtkinter/customtkinter_progressbar.py b/customtkinter/customtkinter_progressbar.py index 003882e..7bfe37c 100644 --- a/customtkinter/customtkinter_progressbar.py +++ b/customtkinter/customtkinter_progressbar.py @@ -46,6 +46,10 @@ class CTkProgressBar(tkinter.Frame): # set progress self.set(self.value) + def destroy(self): + AppearanceModeTracker.remove(self.change_appearance_mode) + super().destroy() + def detect_color_of_master(self): if isinstance(self.master, CTkFrame): return self.master.fg_color diff --git a/customtkinter/customtkinter_slider.py b/customtkinter/customtkinter_slider.py index 9e2a818..18fa482 100644 --- a/customtkinter/customtkinter_slider.py +++ b/customtkinter/customtkinter_slider.py @@ -64,6 +64,10 @@ class CTkSlider(tkinter.Frame): self.draw() + def destroy(self): + AppearanceModeTracker.remove(self.change_appearance_mode) + super().destroy() + def detect_color_of_master(self): if isinstance(self.master, CTkFrame): return self.master.fg_color diff --git a/customtkinter/customtkinter_tk.py b/customtkinter/customtkinter_tk.py index 7112e4f..d49ccba 100644 --- a/customtkinter/customtkinter_tk.py +++ b/customtkinter/customtkinter_tk.py @@ -30,6 +30,7 @@ class CTk(tkinter.Tk): super().configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode)) def destroy(self): + AppearanceModeTracker.remove(self.set_appearance_mode) self.disable_macos_dark_title_bar() super().destroy() @@ -65,6 +66,4 @@ class CTk(tkinter.Tk): elif mode_string.lower() == "light": self.appearance_mode = 0 - print("set",self.bg_color) - super().configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode)) diff --git a/setup.py b/setup.py index 9e46160..81f0f02 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ def read(filename): setup(name="customtkinter", - version="1.5", + version="1.6", author="Tom Schimansky", license="Creative Commons Zero v1.0 Universal", url="https://github.com/TomSchimansky/CustomTkinter",