/
TextEditor.py
227 lines (190 loc) · 8.45 KB
/
TextEditor.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
Python/Tkinter Text Editor
Note that text styles such as color, font, and text size are not preserved
after re-opening a saved file because the file is saved in plain text format
and the tags are properties of the Tkinter text widget.
author: Joe Stepp
last modified: August 2013
github.com/Paradox0
"""
import os
import tkinter
import tkinter.colorchooser
from tkinter import ttk, filedialog, TclError
from tkinter.scrolledtext import ScrolledText
from tkinter.font import Font, families
class Application(tkinter.Tk):
def __init__(self):
"""Initialize widgets, methods."""
tkinter.Tk.__init__(self)
self.grid()
fontoptions = families(self)
font = Font(family="Verdana", size=10)
menubar = tkinter.Menu(self)
fileMenu = tkinter.Menu(menubar, tearoff=0)
editMenu = tkinter.Menu(menubar, tearoff=0)
fsubmenu = tkinter.Menu(editMenu, tearoff=0)
ssubmenu = tkinter.Menu(editMenu, tearoff=0)
# adds fonts to the font submenu and associates lambda functions
for option in fontoptions:
fsubmenu.add_command(label=option, command = lambda: font.configure(family=option))
# adds values to the size submenu and associates lambda functions
for value in range(1,31):
ssubmenu.add_command(label=str(value), command = lambda: font.configure(size=value))
# adds commands to the menus
menubar.add_cascade(label="File",underline=0, menu=fileMenu)
menubar.add_cascade(label="Edit",underline=0, menu=editMenu)
fileMenu.add_command(label="New", underline=1,
command=self.new, accelerator="Ctrl+N")
fileMenu.add_command(label="Open", command=self.open, accelerator="Ctrl+O")
fileMenu.add_command(label="Save", command=self.save, accelerator="Ctrl+S")
fileMenu.add_command(label="Exit", underline=1,
command=exit, accelerator="Ctrl+Q")
editMenu.add_command(label="Copy", command=self.copy, accelerator="Ctrl+C")
editMenu.add_command(label="Cut", command=self.cut, accelerator="Ctrl+X")
editMenu.add_command(label="Paste", command=self.paste, accelerator="Ctrl+V")
editMenu.add_cascade(label="Font", underline=0, menu=fsubmenu)
editMenu.add_cascade(label="Size", underline=0, menu=ssubmenu)
editMenu.add_command(label="Color", command=self.color)
editMenu.add_command(label="Bold", command=self.bold, accelerator="Ctrl+B")
editMenu.add_command(label="Italic", command=self.italic, accelerator="Ctrl+I")
editMenu.add_command(label="Underline", command=self.underline, accelerator="Ctrl+U")
editMenu.add_command(label="Overstrike", command=self.overstrike, accelerator="Ctrl+T")
editMenu.add_command(label="Undo", command=self.undo, accelerator="Ctrl+Z")
editMenu.add_command(label="Redo", command=self.redo, accelerator="Ctrl+Y")
self.config(menu=menubar)
"""Accelerator bindings. The cut, copy, and paste functions are not
bound to keyboard shortcuts because Windows already binds them, so if
Tkinter bound them as well whenever you typed ctrl+v the text would be
pasted twice."""
self.bind_all("<Control-n>", self.new)
self.bind_all("<Control-o>", self.open)
self.bind_all("<Control-s>", self.save)
self.bind_all("<Control-q>", self.exit)
self.bind_all("<Control-b>", self.bold)
self.bind_all("<Control-i>", self.italic)
self.bind_all("<Control-u>", self.underline)
self.bind_all("<Control-T>", self.overstrike)
self.bind_all("<Control-z>", self.undo)
self.bind_all("<Control-y>", self.redo)
self.text = ScrolledText(self, state='normal', height=30, wrap='word', font = font, pady=2, padx=3, undo=True)
self.text.grid(column=0, row=0, sticky='NSEW')
# Frame configuration
self.grid_columnconfigure(0, weight=1)
self.resizable(True, True)
"""Command functions. *args is included because the keyboard bindings pass
two arguments to the functions, while clicking in the menu passes only 1."""
def new(self, *args):
"""Creates a new window."""
app = Application()
app.title('Python Text Editor')
app.option_add('*tearOff', False)
app.mainloop()
def color(self):
"""Changes selected text color."""
try:
(rgb, hx) = tkinter.colorchooser.askcolor()
self.text.tag_add('color', 'sel.first', 'sel.last')
self.text.tag_configure('color', foreground=hx)
except TclError:
pass
def bold(self, *args):
"""Toggles bold for selected text."""
try:
current_tags = self.text.tag_names("sel.first")
if "bold" in current_tags:
self.text.tag_remove("bold", "sel.first", "sel.last")
else:
self.text.tag_add("bold", "sel.first", "sel.last")
bold_font = Font(self.text, self.text.cget("font"))
bold_font.configure(weight="bold")
self.text.tag_configure("bold", font=bold_font)
except TclError:
pass
def italic(self, *args):
"""Toggles italic for selected text."""
try:
current_tags = self.text.tag_names("sel.first")
if "italic" in current_tags:
self.text.tag_remove("italic", "sel.first", "sel.last")
else:
self.text.tag_add("italic", "sel.first", "sel.last")
italic_font = Font(self.text, self.text.cget("font"))
italic_font.configure(slant="italic")
self.text.tag_configure("italic", font=italic_font)
except TclError:
pass
def underline(self, *args):
"""Toggles underline for selected text."""
try:
current_tags = self.text.tag_names("sel.first")
if "underline" in current_tags:
self.text.tag_remove("underline", "sel.first", "sel.last")
else:
self.text.tag_add("underline", "sel.first", "sel.last")
underline_font = Font(self.text, self.text.cget("font"))
underline_font.configure(underline=1)
self.text.tag_configure("underline", font=underline_font)
except TclError:
pass
def overstrike(self, *args):
"""Toggles overstrike for selected text."""
try:
current_tags = self.text.tag_names("sel.first")
if "overstrike" in current_tags:
self.text.tag_remove("overstrike", "sel.first", "sel.last")
else:
self.text.tag_add("overstrike", "sel.first", "sel.last")
overstrike_font = Font(self.text, self.text.cget("font"))
overstrike_font.configure(overstrike=1)
self.text.tag_configure("overstrike", font=overstrike_font)
except TclError:
pass
def undo(self, *args):
"""Undo function"""
try:
self.text.edit_undo()
except TclError:
pass
def redo(self, *args):
"""Redo function"""
try:
self.text.edit_redo()
except TclError:
pass
def copy(self, *args):
"""Copy text"""
self.clipboard_clear()
self.clipboard_append(self.text.selection_get())
def cut(self, *args):
"""Cut text"""
self.copy
self.text.delete("sel.first", "sel.last")
def paste(self, *args):
"""Paste text"""
insertion = self.selection_get(selection = "CLIPBOARD")
self.text.insert(0.0, insertion)
def open(self, *args):
"""Opens a file dialog to open a plain text file."""
filename = tkinter.filedialog.askopenfilename()
with open(filename) as f:
text = f.read()
self.text.delete("1.0", "end")
self.text.insert('insert', text)
def save(self, *args):
try:
"""Opens a file dialog to save the text in plain text format."""
text = self.text.get("1.0", "end")
filename = tkinter.filedialog.asksaveasfilename()
with open(filename, 'w') as f:
f.write(text)
except FileNotFoundError:
pass
def exit(self, *args):
"""Exits the program."""
self.quit()
if __name__ == "__main__":
app=Application()
app.title('Python Text Editor')
app.option_add('*tearOff', False)
app.mainloop()