-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathnotepad.py
More file actions
96 lines (77 loc) · 2.25 KB
/
notepad.py
File metadata and controls
96 lines (77 loc) · 2.25 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
import tkinter as tk
from tkinter import filedialog
import os
def new_file():
global file
root.title("Untitled - Notepad")
file = None
textArea.delete('1.0', tk.END)
def open_file():
global file
file = filedialog.asksaveasfilename(
defaultextension=".txt",
filetypes=[
("All Files", "*.*"),
("Text Document", "*.txt")
]
)
if file == '':
file = None
else:
root.title(os.path.basename(file) + ' - Notepad')
textArea.delete('1.0', tk.END)
f = open(file, 'r')
textArea.insert("1.0", f.read())
f.close()
def save_as_file():
global file
file = filedialog.asksaveasfilename(
initialfile="Untitled.txt",
defaultextension=".txt",
filetypes=[
("All Files", "*.*"),
("Text Document", "*.txt")
]
)
if file == '':
file = None
else:
f = open(file, "w")
f.write(textArea.get('1.0', tk.END))
f.close()
root.title(os.path.basename(file) + ' - Notepad')
def save_file():
global file
if file == None:
save_as_file()
else:
f = open(file, "w")
f.write(textArea.get('1.0', tk.END))
f.close()
def copy():
textArea.event_generate("<<Copy>>")
def cut():
textArea.event_generate("<<Cut>>")
def paste():
textArea.event_generate("<<Paste>>")
root = tk.Tk()
root.geometry("600x400")
root.title('Untitled.txt - Notepad')
file = None
textArea = tk.Text(root, width=600, height=400, fg="black", bg="white")
textArea.pack()
menuBar = tk.Menu(root)
fileMenu = tk.Menu(menuBar, tearoff=0)
fileMenu.add_command(label="New File", command=new_file)
fileMenu.add_command(label="Open File", command=open_file)
fileMenu.add_command(label="Save File", command=save_file)
fileMenu.add_command(label="Save File As...", command=save_as_file)
fileMenu.add_command(label="Exit", command=root.quit)
menuBar.add_cascade(label="File", menu=fileMenu)
editMenu = tk.Menu(menuBar, tearoff=0)
editMenu.add_command(label="Copy", command=copy)
editMenu.add_command(label="Cut", command=cut)
editMenu.add_command(label="Paste", command=paste)
menuBar.add_cascade(label="Edit", menu=editMenu)
root.config(menu=menuBar)
root.mainloop()