-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathedit_menu.py
87 lines (66 loc) · 2.89 KB
/
edit_menu.py
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
81
82
83
84
85
86
87
from tkinter import *
from tkinter.simpledialog import *
from tkinter.filedialog import *
from tkinter.messagebox import *
class Edit():
def popup(self, event):
self.rightClick.post(event.x_root, event.y_root)
def copy(self, *args):
sel = self.text.selection_get()
self.clipboard = sel
def cut(self, *args):
sel = self.text.selection_get()
self.clipboard = sel
self.text.delete(SEL_FIRST, SEL_LAST)
def paste(self, *args):
self.text.insert(INSERT, self.clipboard)
def selectAll(self, *args):
self.text.tag_add(SEL, "1.0", END)
self.text.mark_set(0.0, END)
self.text.see(INSERT)
def undo(self, *args):
self.text.edit_undo()
def redo(self, *args):
self.text.edit_redo()
def find(self, *args):
self.text.tag_remove('found', '1.0', END)
target = askstring('Find', 'Search String:')
if target:
idx = '1.0'
while 1:
idx = self.text.search(target, idx, nocase=1, stopindex=END)
if not idx: break
lastidx = '%s+%dc' % (idx, len(target))
self.text.tag_add('found', idx, lastidx)
idx = lastidx
self.text.tag_config('found', foreground='white', background='blue')
def __init__(self, text, root):
self.clipboard = None
self.text = text
self.rightClick = Menu(root)
def main(root, text, menubar):
objEdit = Edit(text, root)
editmenu = Menu(menubar)
editmenu.add_command(label="Copy", command=objEdit.copy, accelerator="Ctrl+C")
editmenu.add_command(label="Cut", command=objEdit.cut, accelerator="Ctrl+X")
editmenu.add_command(label="Paste", command=objEdit.paste, accelerator="Ctrl+V")
editmenu.add_command(label="Undo", command=objEdit.undo, accelerator="Ctrl+Z")
editmenu.add_command(label="Redo", command=objEdit.redo, accelerator="Ctrl+Y")
editmenu.add_command(label="Find", command=objEdit.find, accelerator="Ctrl+F")
editmenu.add_separator()
editmenu.add_command(label="Select All", command=objEdit.selectAll, accelerator="Ctrl+A")
menubar.add_cascade(label="Edit", menu=editmenu)
root.bind_all("<Control-z>", objEdit.undo)
root.bind_all("<Control-y>", objEdit.redo)
root.bind_all("<Control-f>", objEdit.find)
root.bind_all("Control-a", objEdit.selectAll)
objEdit.rightClick.add_command(label="Copy", command=objEdit.copy)
objEdit.rightClick.add_command(label="Cut", command=objEdit.cut)
objEdit.rightClick.add_command(label="Paste", command=objEdit.paste)
objEdit.rightClick.add_separator()
objEdit.rightClick.add_command(label="Select All", command=objEdit.selectAll)
objEdit.rightClick.bind("<Control-q>", objEdit.selectAll)
text.bind("<Button-3>", objEdit.popup)
root.config(menu=menubar)
if __name__ == "__main__":
print("Please run 'main.py'")