-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_editor.py
More file actions
151 lines (102 loc) · 3.78 KB
/
text_editor.py
File metadata and controls
151 lines (102 loc) · 3.78 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
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
import os
from tkinter import *
from tkinter import filedialog, colorchooser, font
from tkinter.messagebox import *
from tkinter.filedialog import *
def change_color():
color = colorchooser.askcolor(title="Pick a color for the text!")
text_area.config(fg=color[1])
def change_font(*args):
text_area.config(font=(font_name.get(), size_box.get()))
def new_file():
window.title("Untitled")
text_area.delete(1.0, END)
def open_file():
file = askopenfilename(defaultextension=".txt",
file=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file is None:
return
else:
try:
window.title(os.path.basename(file))
text_area.delete(1.0, END)
file = open(file, "r")
text_area.insert(1.0, file.read())
except Exception:
print("couldn't read file")
finally:
file.close()
def save_file():
file = filedialog.asksaveasfilename(initialfile='unititled.txt',
defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file is None:
return
else:
try:
window.title(os.path.basename(file))
file = open(file, "w")
file.write(text_area.get(1.0, END))
except Exception:
print("couldn't save file")
finally:
file.close()
def cut():
text_area.event_generate("<<Cut>>")
def copy():
text_area.event_generate("<<Copy>>")
def paste():
text_area.event_generate("<<Paste>>")
def about():
showinfo("About this program", "Text Editor made by Plextora (github.com/Plextora). Since you read this, here's a secret. I suck at python")
def quit():
window.destroy()
window = Tk()
window.title("Text Editor by Plextora")
file = None
window_width = 500
window_height = 500
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = int((screen_width / 2) - (window_width / 2))
y = int((screen_height / 2) - (window_height / 2))
window.geometry("{}x{}+{}+{}".format(window_width, window_height, x, y))
font_name = StringVar(window)
font_name.set("Arial")
font_size = StringVar(window)
font_size.set("25")
text_area = Text(window, font=(font_name.get(), font_size.get()))
scroll_bar = Scrollbar(text_area)
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
text_area.grid(sticky=N + E + S + W)
scroll_bar.pack(side=RIGHT, fill=Y)
text_area.config(yscrollcommand=scroll_bar.set)
frame = Frame(window)
frame.grid()
color_button = Button(frame, text="color", command=change_color)
color_button.grid(row=0, column=0)
font_box = OptionMenu(frame, font_name, *font.families(), command=change_font)
font_box.grid(row=0, column=1)
size_box = Spinbox(frame, from_=1, to=100, textvariable=font_size, command=change_font)
size_box.grid(row=0, column=2)
menu_bar = Menu(window)
window.config(menu=menu_bar)
file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=quit)
edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=cut)
edit_menu.add_command(label="Copy", command=copy)
edit_menu.add_command(label="Paste", command=paste)
help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=about)
window.mainloop()