Genealogy Apps: Steal this Color Scheme Changing Code
Mar 29, 2023 21:51:58 GMT -8
Post by Uncle Buddy on Mar 29, 2023 21:51:58 GMT -8
This is a working model that will slip right into Treebard's current code while eliminating possibly hundreds of lines of code. Treebard's colorization scheme has worked since the olden times (2018) but it's taken me this long to realize how simple and easy this is. The general ideas shown here have been used for a while but I never realized, while regularly re-writing the color schemes functionality, how much code could be stripped out and how self-contained these custom widget classes could be.
The code shown here uses an imported function `make_formats_dict` to tell the colors what to be, but you can comment out a few lines and rig up a test model using formats1 & formats2 to demonstrate the general idea.
This also changes the ttk.Combobox colors, or some of them. I haven't looked into changing the combobox dropdown colors recently.
The code shown here uses an imported function `make_formats_dict` to tell the colors what to be, but you can comment out a few lines and rig up a test model using formats1 & formats2 to demonstrate the general idea.
This also changes the ttk.Combobox colors, or some of them. I haven't looked into changing the combobox dropdown colors recently.
import tkinter as tk
from tkinter import ttk
from widgets import make_formats_dict
def make_dropdown_menu():
menu = Frame(root, bg="teal")
menu.grid(column=0, row=0, sticky="ew")
file = Menubutton(
menu, text="FILE", font=("arial black", 10),
relief="flat", anchor="w")
file_drop = Menu(
file, tearoff=0, bg="teal", fg="bisque", font=("arial black", 10),
activebackground="black")
file['menu'] = file_drop
file_drop.add_command(label="OPEN", command=open_dlg_1)
file_drop.add_command(label="NEW", command=open_dlg_2)
edit = Menubutton(
menu, text="EDIT", font=("arial black", 10),
relief="flat", anchor="w")
edit_drop = Menu(
edit, tearoff=0, font=("arial black", 10), bd=0)
edit['menu'] = edit_drop
tools = Menubutton(
menu, text="TOOLS", font=("arial black", 10),
relief="flat", anchor="w")
file.grid(column=0, row=0, sticky="w")
edit.grid(column=1, row=0, sticky="w")
tools.grid(column=2, row=0, sticky="w")
# formats1 = {
# "bg": "teal", "highlight_bg": "gray", "head_bg": "steelblue", "fg": "bisque"}
formats2 = {
"bg": "black", "highlight_bg": "purple", "head_bg": "brown", "fg": "skyblue"}
formats = make_formats_dict()
class Frame(tk.Frame):
formats = formats
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.colorize(on_load=True)
def colorize(self, on_load=False):
if on_load is False:
Frame.formats = formats2
self.config(bg=Frame.formats["bg"])
class Toplevel(tk.Toplevel):
formats = formats
def __init__(self, master, *args, **kwargs):
tk.Toplevel.__init__(self, master, *args, **kwargs)
self.colorize(on_load=True)
def colorize(self, on_load=False):
if on_load is False:
Toplevel.formats = formats2
self.config(bg=Toplevel.formats["bg"])
class Label(tk.Label):
formats = formats
def __init__(self, master, *args, **kwargs):
tk.Label.__init__(self, master, *args, **kwargs)
self.bind("<FocusIn>", self.highlight)
self.bind("<FocusOut>", self.unhighlight)
self.config(takefocus=1)
self.colorize(on_load=True)
def highlight(self, evt):
self.config(bg=Label.formats["highlight_bg"])
def unhighlight(self, evt):
self.config(bg=Label.formats["bg"])
def colorize(self, on_load=False):
if on_load is False:
Label.formats = formats2
self.config(bg=Label.formats["bg"], fg=Label.formats["fg"])
class Menubutton(tk.Menubutton):
formats = formats
def __init__(self, master, *args, **kwargs):
tk.Menubutton.__init__(self, master, *args, **kwargs)
self.config(takefocus=1)
self.colorize(on_load=True)
def colorize(self, on_load=False):
if on_load is False:
Menubutton.formats = formats2
self.config(
bg=Menubutton.formats["bg"],
fg=Menubutton.formats["fg"],
activebackground=Menubutton.formats["head_bg"])
class Menu(tk.Menu):
formats = formats
def __init__(self, master, *args, **kwargs):
tk.Menu.__init__(self, master, *args, **kwargs)
self.config(takefocus=1)
self.colorize(on_load=True)
def colorize(self, on_load=False):
if on_load is False:
Menu.formats = formats2
self.config(
bg=Menu.formats["highlight_bg"], fg=Menu.formats["fg"],
activebackground=Menu.formats["head_bg"])
class Button(tk.Button):
formats = formats
def __init__(self, master, *args, **kwargs):
tk.Button.__init__(self, master, *args, **kwargs)
self.config(takefocus=1)
self.colorize(on_load=True)
def colorize(self, on_load=False):
if on_load is False:
Button.formats = formats2
self.config(
bg=Button.formats["bg"], fg=Button.formats["fg"],
activebackground=Button.formats["highlight_bg"])
def config_comboboxes():
cbo_style.configure(
"TCombobox",
fieldbackground=formats["highlight_bg"],
background=formats["head_bg"],
foreground=formats["fg"])
def configall(ancestor, formats):
ancestor_list = []
all_widgets_in_root = get_all_descends(ancestor, ancestor_list)
for i in all_widgets_in_root:
if i.winfo_class() == "TCombobox":
i.select_clear()
else:
i.colorize()
ancestor.config(bg=formats["bg"])
config_comboboxes()
def open_dlg_1():
dlg_1 = Toplevel(root)
dlg_1.geometry("400x400+400+200")
cls1 = Button(dlg_1, text="CLOSE", command=lambda dlg=dlg_1: close(dlg))
cls1.grid()
menubar = Menu(dlg_1, bg="red", font=("arial black", 17))
drop = Menu(menubar, tearoff=0, bg="yellow")
menubar.add_cascade(label="HELP", menu=drop)
menubar.add_cascade(label="ABOUT", menu=drop)
drop.add_command(label="VERSION")
dlg_1.config(menu=menubar)
def open_dlg_2():
dlg_2 = Toplevel(root)
dlg_2.geometry("400x400+850+300")
cls2 = Button(dlg_2, text="CLOSE", command=lambda dlg=dlg_2: close(dlg))
cls2.grid()
def close(dlg):
dlg.destroy()
def get_all_descends (ancestor, deep_list):
'''
So all widgets can be configured at once, this lists every widget in
the app by running recursively.
'''
lst = ancestor.winfo_children()
for item in lst:
deep_list.append(item)
get_all_descends(item, deep_list)
return deep_list
root = tk.Tk()
ttk.Style().theme_use("alt")
make_dropdown_menu()
cbo_style = ttk.Style()
cbo_style.configure(
"TCombobox", fieldbackground= "red", background= "blue",
foreground="green")
cbo = ttk.Combobox(
root,
style="TCombobox",
values=[
("teal", "steelblue", "bisque"),
("magenta", "yellow", "navy"),
("black", "gray", "steelblue")])
cbo.grid()
for i in range(10):
lab = Label(root, text=f"Label Number: {i}")
lab.grid()
b1 = Button(root, text="OPEN DIALOG 1", command=open_dlg_1)
b1.grid()
b2 = Button(root, text="OPEN DIALOG 2", command=open_dlg_2)
b2.grid()
colorizer = Button(
root, text="COLORIZE", command=lambda root=root,
formats=formats: configall(root, formats))
colorizer.grid()
root.mainloop()