Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

How To Make NotePad With Tkinter | Python Tutorial For Free

 

How to make a Notepad using Tkinter Python


How To Make NotePad With Tkinter






Here's The full code :

# 1. We have to import Functions from tkinter import * from tkinter.messagebox import showinfo from tkinter.filedialog import askopenfilename, asksaveasfilename import os
# Making Functions for doing tasks. def newFile(): global file     root.title("Untitled - Notepad")     file = None     TextArea.delete(1.0END) 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.0END)         f = open(file, "r")         TextArea.insert(1.0, f.read())         f.close() 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.0END))             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.0END))         f.close() def quitApp():     root.destroy() def cut():     TextArea.event_generate(("<<Cut>>")) def copy():     TextArea.event_generate(("<<Copy>>")) def paste():     TextArea.event_generate(("<<Paste>>")) def about():     showinfo("Notepad""Notepad by Hyper Programmer ") if __name__ == '__main__': #Basic tkinter setup     root = Tk()     root.title("Untitled - Notepad")     root.geometry("1300x1400") #Add TextArea     TextArea = Text(root, font="lucida 13")     file = None     TextArea.pack(expand=Truefill=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     root.config(menu=MenuBar) #Adding Scrollbar using rules from Tkinter lecture 
    Scroll = Scrollbar(TextArea)     Scroll.pack(side=RIGHT,  fill=Y)     Scroll.config(command=TextArea.yview)     TextArea.config(yscrollcommand=Scroll.set)     root.mainloop()
You can copy the code from here and run in your IDE.

Post a Comment

0 Comments