mirror of
https://github.com/TomSchimansky/CustomTkinter.git
synced 2023-08-10 21:13:13 +03:00
added CTk class which overrides tkinter.Tk for automatic background color changes
This commit is contained in:
parent
afc177ddbb
commit
063c1f8b6a
@ -1,4 +1,4 @@
|
|||||||
__version__ = "1.4"
|
__version__ = "1.5"
|
||||||
|
|
||||||
from .customtkinter_button import CTkButton
|
from .customtkinter_button import CTkButton
|
||||||
from .customtkinter_slider import CTkSlider
|
from .customtkinter_slider import CTkSlider
|
||||||
@ -8,6 +8,7 @@ from .customtkinter_label import CTkLabel
|
|||||||
from .customtkinter_entry import CTkEntry
|
from .customtkinter_entry import CTkEntry
|
||||||
from .customtkinter_dialog import CTkDialog
|
from .customtkinter_dialog import CTkDialog
|
||||||
from .customtkinter_checkbox import CTkCheckBox
|
from .customtkinter_checkbox import CTkCheckBox
|
||||||
|
from .customtkinter_tk import CTk
|
||||||
|
|
||||||
from .appearance_mode_tracker import AppearanceModeTracker, SystemAppearanceModeListenerNoThread
|
from .appearance_mode_tracker import AppearanceModeTracker, SystemAppearanceModeListenerNoThread
|
||||||
from .customtkinter_color_manager import CTkColorManager
|
from .customtkinter_color_manager import CTkColorManager
|
||||||
|
@ -3,6 +3,7 @@ import sys
|
|||||||
|
|
||||||
class CTkColorManager:
|
class CTkColorManager:
|
||||||
|
|
||||||
|
WINDOW_BG = ("#ECECEC", "#323232") # macOS standard light and dark window bg colors
|
||||||
MAIN = ("#1C94CF", "#1C94CF")
|
MAIN = ("#1C94CF", "#1C94CF")
|
||||||
MAIN_HOVER = ("#5FB4DD", "#5FB4DD")
|
MAIN_HOVER = ("#5FB4DD", "#5FB4DD")
|
||||||
ENTRY = ("white", "#222222")
|
ENTRY = ("white", "#222222")
|
||||||
|
70
customtkinter/customtkinter_tk.py
Normal file
70
customtkinter/customtkinter_tk.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import tkinter
|
||||||
|
from distutils.version import StrictVersion as Version
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
|
||||||
|
from .appearance_mode_tracker import AppearanceModeTracker
|
||||||
|
from .customtkinter_color_manager import CTkColorManager
|
||||||
|
|
||||||
|
|
||||||
|
class CTk(tkinter.Tk):
|
||||||
|
def __init__(self, *args,
|
||||||
|
bg_color=CTkColorManager.WINDOW_BG,
|
||||||
|
**kwargs):
|
||||||
|
|
||||||
|
self.enable_macos_dark_title_bar()
|
||||||
|
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||||
|
|
||||||
|
self.bg_color = bg_color
|
||||||
|
if "bg" in kwargs:
|
||||||
|
self.bg_color = kwargs["bg"]
|
||||||
|
del kwargs["bg"]
|
||||||
|
elif "background" in kwargs:
|
||||||
|
self.bg_color = kwargs["background"]
|
||||||
|
del kwargs["background"]
|
||||||
|
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
AppearanceModeTracker.add(self.set_appearance_mode)
|
||||||
|
super().configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode))
|
||||||
|
|
||||||
|
def destroy(self):
|
||||||
|
self.disable_macos_dark_title_bar()
|
||||||
|
super().destroy()
|
||||||
|
|
||||||
|
def config(self, *args, **kwargs):
|
||||||
|
self.configure(*args, **kwargs)
|
||||||
|
|
||||||
|
def configure(self, *args, **kwargs):
|
||||||
|
if "bg" in kwargs:
|
||||||
|
self.bg_color = kwargs["bg"]
|
||||||
|
elif "background" in kwargs:
|
||||||
|
self.bg_color = kwargs["background"]
|
||||||
|
|
||||||
|
super().configure(*args, **kwargs)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def enable_macos_dark_title_bar():
|
||||||
|
if sys.platform == "darwin": # macOS
|
||||||
|
if Version(platform.python_version()) < Version("3.10"):
|
||||||
|
if Version(tkinter.Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
|
||||||
|
os.system("defaults write -g NSRequiresAquaSystemAppearance -bool No")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def disable_macos_dark_title_bar():
|
||||||
|
if sys.platform == "darwin": # macOS
|
||||||
|
if Version(platform.python_version()) < Version("3.10"):
|
||||||
|
if Version(tkinter.Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
|
||||||
|
os.system("defaults delete -g NSRequiresAquaSystemAppearance")
|
||||||
|
# This command reverts the dark-mode setting for all programs.
|
||||||
|
|
||||||
|
def set_appearance_mode(self, mode_string):
|
||||||
|
if mode_string.lower() == "dark":
|
||||||
|
self.appearance_mode = 1
|
||||||
|
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))
|
@ -6,22 +6,14 @@ import sys
|
|||||||
customtkinter.set_appearance_mode("System") # Other: "Light", "Dark"
|
customtkinter.set_appearance_mode("System") # Other: "Light", "Dark"
|
||||||
|
|
||||||
|
|
||||||
class App(tkinter.Tk):
|
class App(customtkinter.CTk):
|
||||||
|
|
||||||
APP_NAME = "CustomTkinter complex example"
|
APP_NAME = "CustomTkinter complex example"
|
||||||
WIDTH = 700
|
WIDTH = 700
|
||||||
HEIGHT = 500
|
HEIGHT = 500
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
customtkinter.enable_macos_darkmode()
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
tkinter.Tk.__init__(self, *args, **kwargs)
|
|
||||||
|
|
||||||
# color the background of the Tk window if mode is not System
|
|
||||||
if customtkinter.get_appearance_mode() == "Dark":
|
|
||||||
self.configure(bg="#323232") # set window background to dark color
|
|
||||||
elif customtkinter.get_appearance_mode() == "Light":
|
|
||||||
self.configure(bg="#ECECEC") # set window background to dark color
|
|
||||||
|
|
||||||
self.title(App.APP_NAME)
|
self.title(App.APP_NAME)
|
||||||
self.geometry(str(App.WIDTH) + "x" + str(App.HEIGHT))
|
self.geometry(str(App.WIDTH) + "x" + str(App.HEIGHT))
|
||||||
@ -164,7 +156,6 @@ class App(tkinter.Tk):
|
|||||||
print("Button pressed")
|
print("Button pressed")
|
||||||
|
|
||||||
def on_closing(self, event=0):
|
def on_closing(self, event=0):
|
||||||
customtkinter.disable_macos_darkmode()
|
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
@ -7,7 +7,7 @@ import sys
|
|||||||
customtkinter.set_appearance_mode("Dark") # Other: "Light", "System"
|
customtkinter.set_appearance_mode("Dark") # Other: "Light", "System"
|
||||||
|
|
||||||
|
|
||||||
class App(tkinter.Tk):
|
class App(customtkinter.CTk):
|
||||||
|
|
||||||
APP_NAME = "CustomTkinter complex example"
|
APP_NAME = "CustomTkinter complex example"
|
||||||
WIDTH = 700
|
WIDTH = 700
|
||||||
@ -18,15 +18,7 @@ class App(tkinter.Tk):
|
|||||||
MAIN_HOVER = "#458577"
|
MAIN_HOVER = "#458577"
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
customtkinter.enable_macos_darkmode()
|
super().__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
tkinter.Tk.__init__(self, *args, **kwargs)
|
|
||||||
|
|
||||||
# color the background of the Tk window if mode is not System
|
|
||||||
if customtkinter.get_appearance_mode() == "Dark":
|
|
||||||
self.configure(bg="#323232") # set window background to dark color
|
|
||||||
elif customtkinter.get_appearance_mode() == "Light":
|
|
||||||
self.configure(bg="#ECECEC") # set window background to dark color
|
|
||||||
|
|
||||||
self.title(App.APP_NAME)
|
self.title(App.APP_NAME)
|
||||||
self.geometry(str(App.WIDTH) + "x" + str(App.HEIGHT))
|
self.geometry(str(App.WIDTH) + "x" + str(App.HEIGHT))
|
||||||
@ -178,7 +170,6 @@ class App(tkinter.Tk):
|
|||||||
print("Button pressed")
|
print("Button pressed")
|
||||||
|
|
||||||
def on_closing(self, event=0):
|
def on_closing(self, event=0):
|
||||||
customtkinter.disable_macos_darkmode()
|
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import tkinter
|
import tkinter
|
||||||
import customtkinter # <- import the CustomTkinter module
|
import customtkinter # <- import the CustomTkinter module
|
||||||
|
|
||||||
customtkinter.enable_macos_darkmode()
|
|
||||||
customtkinter.set_appearance_mode("System") # Other: "Dark", "Light"
|
customtkinter.set_appearance_mode("System") # Other: "Dark", "Light"
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
@ -32,4 +31,3 @@ button_2 = customtkinter.CTkButton(master=frame_1, text="Button_2",
|
|||||||
button_2.place(relx=0.5, rely=0.7, anchor=tkinter.CENTER)
|
button_2.place(relx=0.5, rely=0.7, anchor=tkinter.CENTER)
|
||||||
|
|
||||||
root_tk.mainloop()
|
root_tk.mainloop()
|
||||||
customtkinter.disable_macos_darkmode()
|
|
||||||
|
@ -9,16 +9,14 @@ customtkinter.set_appearance_mode("Dark") # Other: "Light", "System"
|
|||||||
PATH = os.path.dirname(os.path.realpath(__file__))
|
PATH = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
|
|
||||||
class App(tkinter.Tk):
|
class App(customtkinter.CTk):
|
||||||
|
|
||||||
APP_NAME = "CustomTkinter background gardient"
|
APP_NAME = "CustomTkinter background gardient"
|
||||||
WIDTH = 900
|
WIDTH = 900
|
||||||
HEIGHT = 600
|
HEIGHT = 600
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
customtkinter.enable_macos_darkmode()
|
super().__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
tkinter.Tk.__init__(self, *args, **kwargs)
|
|
||||||
|
|
||||||
self.title(App.APP_NAME)
|
self.title(App.APP_NAME)
|
||||||
self.geometry(str(App.WIDTH) + "x" + str(App.HEIGHT))
|
self.geometry(str(App.WIDTH) + "x" + str(App.HEIGHT))
|
||||||
@ -54,7 +52,6 @@ class App(tkinter.Tk):
|
|||||||
print("Button pressed")
|
print("Button pressed")
|
||||||
|
|
||||||
def on_closing(self, event=0):
|
def on_closing(self, event=0):
|
||||||
customtkinter.disable_macos_darkmode()
|
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import tkinter
|
import tkinter
|
||||||
import customtkinter # <- import the CustomTkinter module
|
import customtkinter # <- import the CustomTkinter module
|
||||||
|
|
||||||
customtkinter.enable_macos_darkmode()
|
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window (you can also use normal tkinter.Tk window)
|
||||||
|
|
||||||
root_tk = tkinter.Tk() # create the Tk window like you normally do
|
|
||||||
root_tk.geometry("400x300")
|
root_tk.geometry("400x300")
|
||||||
root_tk.title("CustomTkinter Test")
|
root_tk.title("CustomTkinter Test")
|
||||||
|
|
||||||
@ -46,4 +44,3 @@ checkbox_1 = customtkinter.CTkCheckBox(master=frame_1, command=check_box_functio
|
|||||||
checkbox_1.place(relx=0.5, rely=0.9, anchor=tkinter.CENTER)
|
checkbox_1.place(relx=0.5, rely=0.9, anchor=tkinter.CENTER)
|
||||||
|
|
||||||
root_tk.mainloop()
|
root_tk.mainloop()
|
||||||
customtkinter.disable_macos_darkmode()
|
|
||||||
|
@ -5,10 +5,9 @@ import os
|
|||||||
|
|
||||||
PATH = os.path.dirname(os.path.realpath(__file__))
|
PATH = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
customtkinter.enable_macos_darkmode()
|
|
||||||
customtkinter.set_appearance_mode("System") # Other: "Dark", "Light"
|
customtkinter.set_appearance_mode("System") # Other: "Dark", "Light"
|
||||||
|
|
||||||
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("400x400")
|
root_tk.geometry("400x400")
|
||||||
root_tk.title("CustomTkinter Test")
|
root_tk.title("CustomTkinter Test")
|
||||||
|
|
||||||
@ -47,4 +46,3 @@ button_4.configure(text=None)
|
|||||||
|
|
||||||
|
|
||||||
root_tk.mainloop()
|
root_tk.mainloop()
|
||||||
customtkinter.disable_macos_darkmode()
|
|
||||||
|
2
setup.py
2
setup.py
@ -19,7 +19,7 @@ def read(filename):
|
|||||||
|
|
||||||
|
|
||||||
setup(name="customtkinter",
|
setup(name="customtkinter",
|
||||||
version="1.4",
|
version="1.5",
|
||||||
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",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user