From 5153f99da90970b3d45554e2b8df2e41249ce8bd Mon Sep 17 00:00:00 2001 From: Naman sharma <78961713+Naman23-coder@users.noreply.github.com> Date: Mon, 31 May 2021 18:30:40 +0530 Subject: [PATCH] Create smartpad.py --- Python/smartpad.py | 132 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 Python/smartpad.py diff --git a/Python/smartpad.py b/Python/smartpad.py new file mode 100644 index 0000000..e5f02c0 --- /dev/null +++ b/Python/smartpad.py @@ -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() +