-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmt.py
103 lines (84 loc) · 3.65 KB
/
smt.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from tkinter import *
from tkinter.filedialog import asksaveasfilename
from tkinter.simpledialog import askstring
from tkinter.messagebox import askokcancel
################################################################################
class SimpleEditor(ScrolledText):
def __init__(self, parent=None, file=None):
frm = Frame(parent)
frm.pack(fill=X)
Button(frm, text='Save', command=self.onSave).pack(side=LEFT)
Button(frm, text='Cut', command=self.onCut).pack(side=LEFT)
Button(frm, text='Paste', command=self.onPaste).pack(side=LEFT)
Button(frm, text='Find', command=self.onFind).pack(side=LEFT)
Quitter(frm).pack(side=LEFT)
super().__init__(parent, file=file)
self.text['font'] = 'courier', 9, 'normal'
self.target = ''
def onSave(self):
filename = asksaveasfilename(defaultextension='.txt',
filetypes=(('Text files', '*.txt'),
('Python files', '*.py *.pyw'),
('All files', '*.*')))
if filename:
with open(filename, 'w') as stream:
stream.write(self.gettext())
def onCut(self):
self.clipboard_clear()
self.clipboard_append(self.text.get(SEL_FIRST, SEL_LAST))
self.text.delete(SEL_FIRST, SEL_LAST)
def onPaste(self):
try:
self.text.insert(INSERT, self.selection_get(selection='CLIPBOARD'))
except TclError:
pass
def onFind(self):
self.target = askstring('SimpleEditor', 'Search String?',
initialvalue=self.target)
if self.target:
where = self.text.search(self.target, INSERT, END, nocase=True)
if where:
## print(where)
## self.text.tag_remove(SEL, '1.0', END)
pastit = '{}+{}c'.format(where, len(self.target))
self.text.tag_add(SEL, where, pastit)
self.text.mark_set(INSERT, pastit)
self.text.see(INSERT)
self.text.focus()
################################################################################
class ScrolledText(Frame):
def __init__(self, parent=None, text='', file=None):
super().__init__(parent)
self.pack(expand=YES, fill=BOTH)
self.makewidgets()
self.settext(text, file)
def makewidgets(self):
sbar = Scrollbar(self)
self.text = Text(self, relief=SUNKEN, wrap=WORD)
sbar['command'] = self.text.yview
self.text['yscrollcommand'] = sbar.set
sbar.pack(side=RIGHT, fill=Y)
self.text.pack(side=LEFT, expand=YES, fill=BOTH)
def settext(self, text='', file=None):
if file:
with open(file, 'r') as stream:
text = stream.read()
self.text.delete('1.0', END)
self.text.insert('1.0', text)
self.text.mark_set(INSERT, '1.0')
self.text.focus()
def gettext(self):
return self.text.get('1.0', END + '-1c')
################################################################################
class Quitter(Frame):
def __init__(self, parent=None):
super().__init__(parent)
self.pack()
widget = Button(self, text='Quit', command=self.quit)
widget.pack(expand=YES, fill=BOTH, side=LEFT)
def quit(self):
if askokcancel('Verify exit', 'Really quit?'):
self._root().destroy()
################################################################################
if __name__ == '__main__':
SimpleEditor(file=sys.argv[1] if len(sys.argv) > 1 else None).mainloop()