utilities.py
Nov 25, 2022 18:11:33 GMT -8
Post by Uncle Buddy on Nov 25, 2022 18:11:33 GMT -8
<drive>:\treebard\app\python\utilities.py Last Changed 2023-12-26
# utilities.py
import dev_tools as dt
from dev_tools import look, seeline
def resize_scrollbar(dlg, canvas):
dlg.update_idletasks()
canvas.config(scrollregion=canvas.bbox('all'))
""" DON'T USE THIS `resize_scrolled_content` FOR INSTANCES OF ScrolledDialog which
has its own similar method.
"""
def resize_scrolled_content(toplevel, canvas, window, add_x=0, add_y=0):
""" Besides configuring the scrollbar when the content changes, this
gives a hideable scrollbar a place to grid (scridth) so the
scrollbar doesn't appear before it's needed due to its own
width. Extra space or `scridth` is added where the hidden scrollbars
will appear. Extra spacer frames (such as `scridth_n` and `scridth_w`
in main.py) are added to balance this out (don't do any of this with
padding). The end result is a hideable scrollbar without a lop-sided
border around the canvas.
"""
def resize_window():
""" Don't try to DETECT scrollbar width (`scridth`) in this function.
For some reason it causes certain combinations of values
below to freeze the app. Hard-coded is good enough since there
are only a few sizes of scrollbar. Add 10 to the scrollbar width
and that gives it wiggle room (makes it work right--not sure why).
"""
toplevel.update_idletasks()
if toplevel.winfo_name() == 'tk':
bar_height = 27 # statusbar
scridth = 26
else:
bar_height = 96 # menubar + ribbon + statusbar
scridth = 30
page_x = window.winfo_reqwidth() + scridth + add_x
page_y = window.winfo_reqheight() + scridth + bar_height + add_y
toplevel.geometry(f"{page_x}x{page_y}")
resize_scrollbar(toplevel, canvas)
resize_window()
def fix_tab_traversal(families_table, events_table):
""" Create a tab traversal since the nukefam_table
can't be made first, but should be traversed first.
"""
for table in (families_table, events_table):
table.lift()
def stop_flashing(dlg, scrollbar_width=None):
""" Call when configuring scrollregion if flashing occurs for tiny
images. Doesn't always work if called after configuring scrollregion,
seems more reliable when called before. This was done because the
other way to stop the flashing is to drag the dialog any distance.
"""
dlg.update_idletasks()
size, position = dlg.geometry().split("+", 1)
w, h = size.split("x")
x, y = position.split("+")
if scrollbar_width:
dlg.geometry("{}x{}+{}+{}".format(
str(int(w)+scrollbar_width), h, x, str(int(y)+1)))
else:
dlg.geometry("{}x{}+{}+{}".format(w, h, x, str(int(y)+1)))
def split_sorter(date):
""" Create date sorter. """
sorter = date.split(",")
date = [int(i) for i in sorter]
return date
# capitalizE righT
def titlize(stg):
'''
function by Yugal Jindle. Python's `title()` method doesn't work right
if there are apostrophes etc. in the word, since it breaks words at
punctuation. According to https://bugs.python.org/issue7008, the
`string.capwords()` method is also buggy and should be deprecated. This
is to make "Linda's Tree" not be "Linda'S Tree".
'''
lst = []
for temp in stg.split(" "): lst.append(temp.capitalize())
return ' '.join(lst)
# CENTERING
def center_dialog(dlg, frame=None):
""" I think the `frame` parameter refers to the `create_window` method of
Canvas.
"""
if frame:
dlg.update_idletasks()
win_width = frame.winfo_reqwidth()
win_height = frame.winfo_reqheight()
right_pos = int(dlg.winfo_screenwidth()/2 - win_width/2)
down_pos = int(dlg.winfo_screenheight()/2 - win_height/2) - 50
else:
dlg.update_idletasks()
win_width = dlg.winfo_reqwidth()
win_height = dlg.winfo_reqheight()
print("line", look(seeline()).lineno, "win_width, win_height:", win_width, win_height)
right_pos = int(dlg.winfo_screenwidth()/2 - win_width/2)
down_pos = int(dlg.winfo_screenheight()/2 - win_height/2)
dlg.geometry("+{}+{}".format(right_pos, down_pos))
# # - - - - - - - - - - - - - - - #
# Tkinter keysyms for keys that actually type a character with len(keysym) > 1:
OK_PRINT_KEYS = (
"space", "parenleft", "parenright", "underscore", "minus", "asterisk",
"slash", "period", "comma", "equal", "plus", "ampersand", "asciicircum",
"percent", "dollar", "numbersign", "at", "exclam", "asciitilde",
"quoteleft", "bar", "backslash", "less", "greater", "colon", "semicolon",
"quotedbl", "quoteright", "bracketleft", "braceleft", "bracketright",
"braceright")
# # - - - - - - - - - - - - - - - #
# color strings recognized by Tkinter
COLOR_STRINGS = [
'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige',
'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown',
'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral',
'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan',
'darkgoldenrod', 'darkgray', 'darkgrey', 'darkgreen', 'darkkhaki',
'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred',
'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue',
'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite',
'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod',
'gray', 'grey', 'green', 'greenyellow', 'honeydew', 'hotpink', 'indianred',
'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen',
'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan',
'lightgoldenrodyellow', 'lightgray', 'lightgrey', 'lightgreen',
'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue',
'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow',
'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine',
'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen',
'mediumslateblue', 'mediumspringgreen', 'mediumturquoise',
'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin',
'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange',
'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise',
'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum',
'powderblue', 'purple', 'red', 'rosybrown', 'royalblue', 'saddlebrown',
'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver',
'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow', 'springgreen',
'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet',
'wheat', 'white', 'whitesmoke', 'yellow']