-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotepad(simple).py
More file actions
110 lines (90 loc) · 3.25 KB
/
Notepad(simple).py
File metadata and controls
110 lines (90 loc) · 3.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from tkinter import *
from tkinter import messagebox as msg
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os
# Command for menus-
def newFile():
global crfile
crfile = None
txtarea.delete(1.0,END)
def saveFile():
global crfile
if crfile == None:
crfile = asksaveasfilename(initialfile="Untitled.txt",
defaultextension=".txt"
, filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if crfile == "":
crfile = None
else:
# Save as new file
with open(crfile, "w") as f:
f.write(txtarea.get(1.0, END))
root.title(os.path.basename(crfile + "- Tanish's Notepad"))
else:
# Save the file
with open(crfile, "w") as f:
f.write(txtarea.get(1.0, END))
root.title(os.path.basename(crfile + "- Tanish's Notepad"))
def openFile():
global crfile
crfile = askopenfilename(defaultextension=".txt"
, filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if crfile == "":
crfile = None
else:
root.title(os.path.basename(crfile) + "-" + "Tanish's Notepad")
txtarea.delete(1.0, END)
with open(crfile,"r") as f:
txtarea.insert(1.0,f.read())
# commands for edit menu-
def copy():
txtarea.event_generate(("<<Copy>>"))
def paste():
txtarea.event_generate(("<<Paste>>"))
def cut():
txtarea.event_generate(("<<Cut>>"))
#commands for help menu-
def About():
msg.showinfo("Tanish's Notepad -Help","This is a simple notepad made by Tanish Sarmah")
if __name__ == "__main__":
# Basic tkinter startup
root = Tk()
root.geometry("800x700")
root.title("Tanish's Notepad-Untitled-1")
root.wm_iconbitmap("noteicon.ico")
# TODO COMMANDS----
# TODO Menus
mainmenu = Menu(root, tearoff="0")
# file menu
file = Menu(mainmenu, tearoff=0)
file.add_command(label="New", command=newFile) # CCreating new file
file.add_command(label="Save", command=saveFile) # Saving the current file
# Opening a Pre-Existing file
file.add_command(label="Open", command=openFile)
file.add_separator()
file.add_command(label="Exit", command=root.destroy) #Exit command
mainmenu.add_cascade(menu=file, label="File")
root.config(menu=mainmenu)
# Edit menu---
edit = Menu(mainmenu, tearoff=0)
edit.add_command(label="Cut", command=cut) #To cut any part of the Textarea
edit.add_command(label="Copy", command=copy) #To copy any part of the Textarea
edit.add_command(label="Paste", command=paste) #To paste any cut or copied Text.
mainmenu.add_cascade(menu=edit, label="Edit")
root.config(menu=mainmenu)
# Help menu---
helpm = Menu(mainmenu, tearoff=0)
helpm.add_command(label="About",command=About) #Displays about the notepad
mainmenu.add_cascade(menu=helpm, label="Help")
root.config(menu=mainmenu)
# TODO Text widget---
global scbar
txtarea = Text(root)
crfile = None
txtarea.pack(expand=True, fill=BOTH)
# TODO Scrollbar---
scbar = Scrollbar(txtarea)
txtarea.config(yscrollcommand=scbar.set)
scbar.config(command=txtarea.yview)
scbar.pack(side=RIGHT,fill=BOTH)
root.mainloop()