-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui_utils.py
More file actions
80 lines (70 loc) · 3.15 KB
/
Copy pathui_utils.py
File metadata and controls
80 lines (70 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import tkinter as tk
def add_context_menu(widget):
"""Добавляет контекстное меню и горячие клавиши (копировать/вставить/вырезать) с поддержкой русской раскладки."""
# --- Функции для работы с буфером обмена ---
def copy_to_clipboard(event=None):
try:
selected = widget.selection_get()
widget.clipboard_clear()
widget.clipboard_append(selected)
except tk.TclError:
pass
return "break"
def cut_to_clipboard(event=None):
try:
selected = widget.selection_get()
widget.clipboard_clear()
widget.clipboard_append(selected)
widget.delete("sel.first", "sel.last")
except tk.TclError:
pass
return "break"
def paste_from_clipboard(event=None):
try:
text = widget.clipboard_get()
widget.insert(tk.INSERT, text)
except tk.TclError:
pass
return "break"
def select_all(event=None):
if hasattr(widget, 'tag_add'):
widget.tag_add(tk.SEL, "1.0", tk.END)
else:
widget.select_range(0, tk.END)
return "break"
# --- Универсальный обработчик для Ctrl+любая клавиша ---
def on_ctrl_key(event):
# Проверяем, зажат ли Ctrl
if event.state & 0x4:
# keycode 67 — это клавиша 'C/С', 86 — 'V/М', 88 — 'X/Ч', 65 — 'A/Ф'
if event.keycode == 67: # Клавиша C
copy_to_clipboard()
elif event.keycode == 88: # Клавиша X
cut_to_clipboard()
elif event.keycode == 86: # Клавиша V
paste_from_clipboard()
elif event.keycode == 65: # Клавиша A
select_all()
return "break"
# Привязываем наш обработчик ко всем нажатиям клавиш с зажатым Ctrl
widget.bind('<Control-KeyPress>', on_ctrl_key)
# --- Контекстное меню (правой кнопкой мыши) ---
menu = tk.Menu(widget, tearoff=0)
menu.add_command(label="Вырезать", command=cut_to_clipboard)
menu.add_command(label="Копировать", command=copy_to_clipboard)
menu.add_command(label="Вставить", command=paste_from_clipboard)
def show_menu(event):
try:
if widget.selection_present():
menu.entryconfig("Вырезать", state="normal")
menu.entryconfig("Копировать", state="normal")
else:
menu.entryconfig("Вырезать", state="disabled")
menu.entryconfig("Копировать", state="disabled")
menu.entryconfig("Вставить", state="normal")
except:
pass
menu.tk_popup(event.x_root, event.y_root)
return "break"
widget.bind("<Button-3>", show_menu) # Для Windows/Linux
widget.bind("<Button-2>", show_menu) # Для macOS