Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions Python/smartpad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
## (c) Naman Sharma 2021
## this is a notepad clone called smartpad which saves it's text as .smp file format
"""
requirments:-
1.Pyautogui (pip install pyautogui)

"""
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os
import pyautogui

def newFile():
global file
root.title("Untitled - SmartPad")
file = None
TextArea.delete(1.0, END)


def openFile():
global file
file = askopenfilename(defaultextension=".smp",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.smp")])
if file == "":
file = None
else:
root.title(os.path.basename(file) + " - SmartPad")
TextArea.delete(1.0, END)
f = open(file, "r")
TextArea.insert(1.0, f.read())
f.close()


def saveFile():
global file
if file == None:
file = asksaveasfilename(initialfile = 'Untitled.smp', defaultextension=".smp",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.smp")])
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) + " - SmartPad")
print("File Saved")
else:
# Save the file
f = open(file, "w")
f.write(TextArea.get(1.0, END))
f.close()

def quitApp():
root.destroy()

def cut():
pyautogui.hotkey('ctrl', 'x')

def copy():
pyautogui.hotkey('ctrl', 'c')

def paste():
pyautogui.hotkey('ctrl', 'v')

def about():
showinfo("SmartPad", "SmartPad by Naman Sharma")


if __name__ == '__main__':
#Basic tkinter setup
root = Tk()
root.title("Untitled - SmartPad")
# root.wm_iconbitmap("1.ico") # icon of your choice can be added
root.geometry("644x788")

#Add TextArea
TextArea = Text(root, font="Arial 13")
file = None
TextArea.pack(expand=True, fill=BOTH)

# Lets create a menubar
MenuBar = Menu(root)

#File Menu Starts
FileMenu = Menu(MenuBar, tearoff=0)
# To open new file
FileMenu.add_command(label="New", command=newFile)

#To Open already existing file
FileMenu.add_command(label="Open", command = openFile)

# To save the current file

FileMenu.add_command(label = "Save", command = saveFile)
FileMenu.add_separator()
FileMenu.add_command(label = "Exit", command = quitApp)
MenuBar.add_cascade(label = "File", menu=FileMenu)
# File Menu ends

# Edit Menu Starts
EditMenu = Menu(MenuBar, tearoff=0)
#To give a feature of cut, copy and paste
EditMenu.add_command(label = "Cut", command=cut)
EditMenu.add_command(label = "Copy", command=copy)
EditMenu.add_command(label = "Paste", command=paste)

MenuBar.add_cascade(label="Edit", menu = EditMenu)

# Edit Menu Ends

# Help Menu Starts
HelpMenu = Menu(MenuBar, tearoff=0)
HelpMenu.add_command(label = "About Notepad", command=about)
MenuBar.add_cascade(label="Help", menu=HelpMenu)


# Help Menu Ends

#Adding Scrollbar using rules from Tkinter lecture no 22
Scroll = Scrollbar(TextArea)
Scroll.pack(side=RIGHT, fill=Y)
Scroll.config(command=TextArea.yview)
TextArea.config(yscrollcommand=Scroll.set)

root.mainloop()