readme update for CTk class and bug fix for .destroy() for all widgets

This commit is contained in:
Tom Schimansky 2022-01-01 22:16:06 +01:00
parent 063c1f8b6a
commit af73c9ad5f
12 changed files with 90 additions and 17 deletions

View File

@ -35,7 +35,7 @@ To test customtkinter you can try this simple example with only a single button:
import tkinter import tkinter
import customtkinter # <- import the CustomTkinter module 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.geometry("400x240")
root_tk.title("CustomTkinter Test") 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) button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
``` ```
### How to use macOS dark mode? ### Dark mode and dark title-bar on macOS
If you have a python version with Tcl/Tk >= 8.6.9, then you can enable the macOS If you have a python version with Tcl/Tk >= 8.6.9, then you automatically get
darkmode. Currently only the anaconda python versions have Tcl/Tk >= 8.6.9. a dark title bar with macOS dark-mode on, if you use the `customtkinter.Ctk` class to create
So if you want a dark window titlebar, you have to install anaconda python version 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. or miniconda.
```python ```python
import tkinter import tkinter
import customtkinter import customtkinter
customtkinter.enable_macos_darkmode()
customtkinter.set_appearance_mode("System") customtkinter.set_appearance_mode("System")
root_tk = customtkinter.CTk()
... the program ... ... 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) ![](documentation_images/simple_macOS_darkmode_test.png)
@ -96,14 +98,14 @@ the System mode:
### Advanced example with multiple CTkFrames ### Advanced example with multiple CTkFrames
Here I used the ``customtkinter.enable_macos_darkmode()`` command to Here I used the ``customtkinter.CTk()`` class to create the main window with two CTkFrame's and
enable the macOS darkmode, and used multpiple CTkFrames. It has some set the appearance mode to `System`. It has some
kind of a menu on the left side, and I used all CustomTkinter elements 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 there are at the moment.Maybe this is a good reference if you want to
create your own application with this library. create your own application with this library.
(Code: /complex_example.py) (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) ![](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 ## 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()
```
<details>
<summary>Show all arguments:</summary>
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=(<light-mode color>, <dark-mode color>))
```
### CTkButton ### CTkButton
Examle Code: Examle Code:
```python ```python

View File

@ -1,4 +1,4 @@
__version__ = "1.5" __version__ = "1.6"
from .customtkinter_button import CTkButton from .customtkinter_button import CTkButton
from .customtkinter_slider import CTkSlider from .customtkinter_slider import CTkSlider

View File

@ -119,6 +119,10 @@ class AppearanceModeTracker():
def add(cls, callback): def add(cls, callback):
cls.callback_list.append(callback) cls.callback_list.append(callback)
@classmethod
def remove(cls, callback):
cls.callback_list.remove(callback)
@classmethod @classmethod
def get_mode(cls): def get_mode(cls):
return cls.appearance_mode return cls.appearance_mode
@ -141,10 +145,19 @@ class AppearanceModeTracker():
if cls.appearance_mode == 0: if cls.appearance_mode == 0:
for callback in cls.callback_list: for callback in cls.callback_list:
try:
callback("Light") callback("Light")
except Exception:
print("error callback")
continue
elif cls.appearance_mode == 1: elif cls.appearance_mode == 1:
for callback in cls.callback_list: for callback in cls.callback_list:
try:
callback("Dark") callback("Dark")
except Exception:
print("error callback")
continue
AppearanceModeTracker.init_listener_function() AppearanceModeTracker.init_listener_function()

View File

@ -101,6 +101,10 @@ class CTkButton(tkinter.Frame):
self.bind('<Configure>', self.update_dimensions) self.bind('<Configure>', self.update_dimensions)
self.draw() self.draw()
def destroy(self):
AppearanceModeTracker.remove(self.set_appearance_mode)
super().destroy()
def configure_basic_grid(self): 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 # 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) self.grid_rowconfigure(0, weight=1)

View File

@ -91,6 +91,10 @@ class CTkCheckBox(tkinter.Frame):
self.draw() self.draw()
def destroy(self):
AppearanceModeTracker.remove(self.set_appearance_mode)
super().destroy()
def detect_color_of_master(self): def detect_color_of_master(self):
if isinstance(self.master, CTkFrame): if isinstance(self.master, CTkFrame):
return self.master.fg_color return self.master.fg_color

View File

@ -54,6 +54,10 @@ class CTkEntry(tkinter.Frame):
self.draw() self.draw()
def destroy(self):
AppearanceModeTracker.remove(self.change_appearance_mode)
super().destroy()
def detect_color_of_master(self): def detect_color_of_master(self):
if isinstance(self.master, CTkFrame): if isinstance(self.master, CTkFrame):
return self.master.fg_color return self.master.fg_color

View File

@ -58,6 +58,10 @@ class CTkFrame(tkinter.Frame):
self.draw() self.draw()
def destroy(self):
AppearanceModeTracker.remove(self.change_appearance_mode)
super().destroy()
def detect_color_of_master(self): def detect_color_of_master(self):
if isinstance(self.master, CTkFrame): if isinstance(self.master, CTkFrame):
return self.master.fg_color return self.master.fg_color

View File

@ -69,6 +69,10 @@ class CTkLabel(tkinter.Frame):
self.draw() self.draw()
def destroy(self):
AppearanceModeTracker.remove(self.change_appearance_mode)
super().destroy()
def detect_color_of_master(self): def detect_color_of_master(self):
if isinstance(self.master, CTkFrame): if isinstance(self.master, CTkFrame):
return self.master.fg_color return self.master.fg_color

View File

@ -46,6 +46,10 @@ class CTkProgressBar(tkinter.Frame):
# set progress # set progress
self.set(self.value) self.set(self.value)
def destroy(self):
AppearanceModeTracker.remove(self.change_appearance_mode)
super().destroy()
def detect_color_of_master(self): def detect_color_of_master(self):
if isinstance(self.master, CTkFrame): if isinstance(self.master, CTkFrame):
return self.master.fg_color return self.master.fg_color

View File

@ -64,6 +64,10 @@ class CTkSlider(tkinter.Frame):
self.draw() self.draw()
def destroy(self):
AppearanceModeTracker.remove(self.change_appearance_mode)
super().destroy()
def detect_color_of_master(self): def detect_color_of_master(self):
if isinstance(self.master, CTkFrame): if isinstance(self.master, CTkFrame):
return self.master.fg_color return self.master.fg_color

View File

@ -30,6 +30,7 @@ class CTk(tkinter.Tk):
super().configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode)) super().configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode))
def destroy(self): def destroy(self):
AppearanceModeTracker.remove(self.set_appearance_mode)
self.disable_macos_dark_title_bar() self.disable_macos_dark_title_bar()
super().destroy() super().destroy()
@ -65,6 +66,4 @@ class CTk(tkinter.Tk):
elif mode_string.lower() == "light": elif mode_string.lower() == "light":
self.appearance_mode = 0 self.appearance_mode = 0
print("set",self.bg_color)
super().configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode)) super().configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode))

View File

@ -19,7 +19,7 @@ def read(filename):
setup(name="customtkinter", setup(name="customtkinter",
version="1.5", version="1.6",
author="Tom Schimansky", author="Tom Schimansky",
license="Creative Commons Zero v1.0 Universal", license="Creative Commons Zero v1.0 Universal",
url="https://github.com/TomSchimansky/CustomTkinter", url="https://github.com/TomSchimansky/CustomTkinter",