""" Ctrl+Z / Ctrl+Y undo/redo for CTkEntry widgets. tk.Entry has no built-in undo, so we track history manually. """ def enable_undo(ctk_entry): """Add Ctrl+Z undo and Ctrl+Y redo to a CTkEntry widget.""" entry = ctk_entry._entry history = [entry.get()] redo_stack = [] _recording = [True] def _snapshot(*_args): if not _recording[0]: return val = entry.get() if not history or history[-1] != val: history.append(val) redo_stack.clear() if len(history) > 200: del history[:100] def _undo(event): if len(history) > 1: redo_stack.append(history.pop()) _recording[0] = False entry.delete(0, "end") entry.insert(0, history[-1]) _recording[0] = True elif history: redo_stack.append(history.pop()) _recording[0] = False entry.delete(0, "end") _recording[0] = True return "break" def _redo(event): if redo_stack: val = redo_stack.pop() history.append(val) _recording[0] = False entry.delete(0, "end") entry.insert(0, val) _recording[0] = True return "break" entry.bind("", _snapshot, add="+") entry.bind("", _undo) entry.bind("", _redo)