это работает, имея главный словарь (не обязательно, только для " «режимы быстрого доступа» предполагает) и подсловарь с ключом в качестве события и значением в качестве функции.
есть также некоторые условия для того, что привязывать, если для события требуются двойные шевроны и, наконец, если это специальный символ, который заканчивается буквенным символом, сделайте привязку события как к верхнему, так и к нижнему символу.
это функция:
Код: Выделить всё
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[1]
bind_to = self
if mode == 'auto':
bind_to = self.EgonTE # bind to specific widget
elif command == '':
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'', 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)
Моя проблема в том, что, хотя код работает нормально, без ошибок и работает гладко, когда я тестирую ярлыки ( обязательность) они не работают.
Подробнее здесь: https://stackoverflow.com/questions/787 ... oesnt-work