-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad.py
More file actions
94 lines (91 loc) · 2.9 KB
/
notepad.py
File metadata and controls
94 lines (91 loc) · 2.9 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
#import required lib
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter.filedialog import askopenfilename , asksaveasfilename
import os
#function to create the file
def newFile():
global file
root.title("Untitled - Notepad")
file = None
textarea.delete(1.0, END)
#function to save the file
def openFile():
global file
file = askopenfilename(defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file == "":
file = None
else:
root.title(os.path.basename(file) + " - Notepad")
textarea.delete(1.0, END)
f = open(file, "r")
textarea.insert(1.0, f.read())
f.close()
#function to save the file
def saveFile():
global file
if file == None:
file = asksaveasfilename(initialfile = 'Untitled.txt', defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file =="":
file = None
else:
#Save as a new file
f = open(file, "w")
f.write(textarea.get(1.0, END))
f.close()
root.title(os.path.basename(file) + " - Notepad")
print("File Saved")
else:
# Save the file
f = open(file, "w")
f.write(textarea.get(1.0, END))
f.close()
#for cut
def cut():
print(textarea.event_generate("<<Cut>>"))
#for copy
def copy():
print(textarea.event_generate("<<Copy>>"))
#for paste
def paste():
print(textarea.event_generate("<<paste>>"))
#for about
def about():
showinfo("Notepad","by deepanshu tyagi")
#main program
if __name__ == "__main__":
root = Tk()
root.geometry("500x500")
root.title("Untitled - Notepad")
menu1 = Menu(root)
root.config(menu=menu1)
#filemenu
filemenu = Menu(root, tearoff=0)
filemenu.add_command(label="New", command=newFile)
filemenu.add_command(label="Open", command=openFile)
filemenu.add_command(label="Save", command=saveFile)
filemenu.add_command(label="Exit", command=exit)
menu1.add_cascade(label="File", menu=filemenu)
#editmenu
editmenu = Menu(root, tearoff=0)
editmenu.add_command(label="Cut", command=cut)
editmenu.add_command(label="Copy", command=copy)
editmenu.add_command(label="Paste", command=paste)
menu1.add_cascade(label="Edit", menu=editmenu)
#aboutmenu
aboutmenu = Menu(root, tearoff=0)
aboutmenu.add_command(label="about", command=about)
menu1.add_cascade(label="Help", menu=aboutmenu)
#textarea
textarea = Text(root, font="lucida 13")
textarea.pack(expand=True, fill='both')
#scrollbar
scrollbar = Scrollbar(textarea)
scrollbar.pack(side=RIGHT, fill=Y)
scrollbar.config(command=textarea.yview)
textarea.config(yscrollcommand=scrollbar.set)
root.mainloop()