I’ve tried to modify my code and make the assignments of binding events and shortcut to function via loop.
it work by having a master dictionary (not necessary, just for “shortcut modes” preposes) and a sub-dictionary with the key as the event and the value as the function.
there is also some conditions for what to bind, if the event needs double chevrons and lastly if it’s custom one that ends with a alphabetical character – make it bind event both with that character upper and lower.
this is the function:
from large_variables import * # this is where we get the the characters_str - a list of upper alphabetic characters
from tkinter import *
class Window(Tk):
def __init__(self):
super().__init__()
....
self.binds('initial')
def binds(self, mode : str):
# modes dicts
initial_dict = {'Modified' : self.status, 'Cut' : lambda event: self.cut(True), 'Copy' : self.copy(True), 'Control-Key-a' : self.select_all,
'Control-Key-l' : self.align_text, 'Control-Key-e' : lambda: self.align_text('center'), 'Control-Key-r' : lambda: self.align_text('right'),
'Control-Key-Delete' : self.clear, 'Alt-F4' : self.exit_app, 'Control-Key-plus' : self.sizes_shortcuts,
'Control-Key-minus' : self.sizes_shortcuts(-1), 'F5' : self.dt}
auto_dict, autol_dict = {'KeyPress' : self.emoji_detection, 'KeyRelease' : self.update_insert_image_list}, {'KeyRelease': self.aul_var}
typef_dict = {'Control-Key-b' : lambda e: self.typefaces(tf='bold'), 'Control-Key-i' : lambda e: self.typefaces(tf='italics'),
'Control-Key-u' : lambda e: self.typefaces(tf='underline')}
editf_dict = {'Control-Key-f' : self.find_text, 'Control-Key-h' : self.replace, 'Control-Key-g' : self.goto}
filea_dict = {'Control-Key-p' : self.print_file, 'Control-Key-n' : self.new_file, 'Alt-Key-d' : self.copy_file_path,
'Control-o' : self.open_file, 'Control-Key-s' : self.save}
textt_dict = {'Control-Shift-Key-j' : self.join_words, 'Control-Shift-Key-u' : self.lower_upper, 'Control-Shift-Key-r' : self.reverse_characters,
'Control-Shift-Key-c' : self.reverse_words}
win_dict = {'F11' : self.full_screen, 'Control-Key-t' : self.topmost}
# master dict
bind_dict = {'initial' : initial_dict, 'autof' : auto_dict, 'typef' : typef_dict, 'editf' : editf_dict,
'filea' : filea_dict, 'textt' : textt_dict, 'windf' : win_dict, 'autol' : autol_dict}
# specific mode or all
if not(mode == 'initial' or mode == 'reset'):
chosen_dict = bind_dict[mode]
else:
chosen_dict = {}
for subdict in bind_dict.values():
for key, value in subdict.items():
chosen_dict[key] = value
for index, bond in enumerate(chosen_dict.items()):
command, function = f'<{bond[0]}>', bond[1]
bind_to = self
if mode == 'auto':
bind_to = self.EgonTE # bind to specific widget
elif command == '<ComboboxSelected>':
bind_to = self.font_size # bind to specific widget
if index > 0:
bind_to = self.font_ui # bind to specific widget
default_bind = True
if command[-3] == '-' and command[-2].upper() in characters_str:
bind_to.bind(f'{command[:-1]}{command[-1].upper()}', function) # condition for both last's char being in lower and upper case
else:
if not(command[-1].isdigit()): # condition for double chevrons
bind_to.bind(f'<{command}>', function)
default_bind = False
if default_bind: # mainly to complete the last's char being in lower and upper case
bind_to.bind(command, function)
as you can see, also there’s also modes to configure what to turn on (via another function there’s unbinding and in here we bind again based on the chosen “mode”) – but we don’t need to take a look about that here – it won’t change anything.
my problem is that although the code works fine and without errors and run smoothly when I test the shortcuts (binding) they do not work.