-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpytexteditor.py
More file actions
72 lines (43 loc) · 1.74 KB
/
Copy pathpytexteditor.py
File metadata and controls
72 lines (43 loc) · 1.74 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
from tkinter import *
import tkinter.filedialog
class TextEditor:
@staticmethod
def quit_app(event=None):
root.quit()
def open_file(self, event=None):
txt_file = tkinter.filedialog.askopenfilename(parent=root, initialdir="/",title = "Select file")
if txt_file:
self.text_area.delete(1.0, END)
with open(txt_file) as _file:
self.text_area.insert(1.0, _file.read())
root.update_idletasks()
def save_file(self, event=None):
file = tkinter.fiedialog.asksaveasfile(mode='w')
if file != None:
data = self.text_area.get('1.0', END + '-1c')
file.write(data)
file.close()
def __init__(self, root):
self.text_to_write = ""
root.title("Text Editor")
root.geometry("600x550")
frame = Frame(root, width=600, height=550)
scrollbar = Scrollbar(frame)
self.text_area = Text(frame, width=600, height=550,
yscrollcommand=scrollbar.set,
padx=10, pady=10)
scrollbar.config(command=self.text_area.yview)
scrollbar.pack(side="right", fill="y")
self.text_area.pack(side="left", fill="both", expand=True)
frame.pack()
the_menu = Menu(root)
file_menu = Menu(the_menu, tearoff=0)
file_menu.add_command(label="Open", command=self.open_file)
file_menu.add_command(label="Save", command=self.save_file)
file_menu.add_separator()
file_menu.add_command(label="Quit", command=self.quit_app)
the_menu.add_cascade(label="File", menu=file_menu)
root.config(menu=the_menu)
root =Tk()
text_editor = TextEditor(root)
root.mainloop()