GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Develop Notepad using Tkinter in python ![]() Article Categories
Selected Reading
Develop Notepad using Tkinter in pythonTkinter is a standard GUI library in Python that enables developers to create desktop applications. In this tutorial, we'll build a fully functional notepad text editor with file operations, editing features, and menu functionality using Tkinter. Prerequisites
Note: Tkinter is included as a standard library with Python 3.x installations. Notepad Menu StructureOur notepad will contain four main menu categories with their respective sub-items:
Creating Menu ItemsHere's how we add menu items and their functionality ? # File menu commands self.__thisFileMenu.add_command(label="New", command=self.__newFile) self.__thisFileMenu.add_command(label="Open", command=self.__openFile) self.__thisFileMenu.add_command(label="Save", command=self.__saveFile) self.__thisFileMenu.add_separator() self.__thisFileMenu.add_command(label="Exit", command=self.__quitApplication) self.__thisMenuBar.add_cascade(label="File", menu=self.__thisFileMenu) # Edit menu commands self.__thisEditMenu.add_command(label="Cut", command=self.__cut) self.__thisEditMenu.add_command(label="Copy", command=self.__copy) self.__thisEditMenu.add_command(label="Paste", command=self.__paste) self.__thisMenuBar.add_cascade(label="Edit", menu=self.__thisEditMenu) # Help and Commands menus self.__thisHelpMenu.add_command(label="About Notepad", command=self.__showAbout) self.__thisCommandMenu.add_command(label="About Commands", command=self.__showCommand) self.__thisMenuBar.add_cascade(label="Commands", menu=self.__thisCommandMenu) self.__thisMenuBar.add_cascade(label="Help", menu=self.__thisHelpMenu) Implementing Menu FunctionsEach menu item requires a corresponding function to handle its operation ? def __quitApplication(self):
self.__root.destroy()
def __showAbout(self):
showinfo("About Notepad", "Simple text editor like notepad using Python")
def __showCommand(self):
showinfo("Notepad", "Just Another TextPad \n Copyright \n with BSD license")
def __openFile(self):
self.__file = askopenfilename(defaultextension=".txt",
filetypes=[("All Files","*.*"),
("Text Documents","*.txt")])
if self.__file == "":
self.__file = None
else:
self.__root.title(os.path.basename(self.__file) + " - Notepad")
self.__thisTextArea.delete(1.0, END)
with open(self.__file, "r") as file:
self.__thisTextArea.insert(1.0, file.read())
def __newFile(self):
self.__root.title("Untitled - Notepad")
self.__file = None
self.__thisTextArea.delete(1.0, END)
def __saveFile(self):
if self.__file == None:
self.__file = asksaveasfilename(initialfile='Untitled.txt',
defaultextension=".txt",
filetypes=[("All Files","*.*"),
("Text Documents","*.txt")])
if self.__file != "":
with open(self.__file, "w") as file:
file.write(self.__thisTextArea.get(1.0, END))
self.__root.title(os.path.basename(self.__file) + " - Notepad")
def __cut(self):
self.__thisTextArea.event_generate("<<Cut>>")
def __copy(self):
self.__thisTextArea.event_generate("<<Copy>>")
def __paste(self):
self.__thisTextArea.event_generate("<<Paste>>")
Complete Notepad ApplicationHere's the complete code for our Tkinter notepad application ? import os
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
class Notepad:
# Initialize window components
__root = Tk()
__thisWidth = 500
__thisHeight = 700
__thisTextArea = Text(__root)
__thisMenuBar = Menu(__root)
__thisFileMenu = Menu(__thisMenuBar, tearoff=0)
__thisEditMenu = Menu(__thisMenuBar, tearoff=0)
__thisHelpMenu = Menu(__thisMenuBar, tearoff=0)
__thisCommandMenu = Menu(__thisMenuBar, tearoff=0)
__thisScrollBar = Scrollbar(__thisTextArea)
__file = None
def __init__(self, **kwargs):
# Set window icon (optional)
try:
self.__root.wm_iconbitmap("Notepad.ico")
except:
pass
# Set window dimensions
try:
self.__thisWidth = kwargs['width']
except KeyError:
pass
try:
self.__thisHeight = kwargs['height']
except KeyError:
pass
# Set window title and center it
self.__root.title("Untitled - Notepad")
screenWidth = self.__root.winfo_screenwidth()
screenHeight = self.__root.winfo_screenheight()
left = (screenWidth / 2) - (self.__thisWidth / 2)
top = (screenHeight / 2) - (self.__thisHeight / 2)
self.__root.geometry('%dx%d+%d+%d' % (self.__thisWidth, self.__thisHeight, left, top))
# Make textarea resizable
self.__root.grid_rowconfigure(0, weight=1)
self.__root.grid_columnconfigure(0, weight=1)
self.__thisTextArea.grid(sticky=N + E + S + W)
# Create File menu
self.__thisFileMenu.add_command(label="New", command=self.__newFile)
self.__thisFileMenu.add_command(label="Open", command=self.__openFile)
self.__thisFileMenu.add_command(label="Save", command=self.__saveFile)
self.__thisFileMenu.add_separator()
self.__thisFileMenu.add_command(label="Exit", command=self.__quitApplication)
self.__thisMenuBar.add_cascade(label="File", menu=self.__thisFileMenu)
# Create Edit menu
self.__thisEditMenu.add_command(label="Cut", command=self.__cut)
self.__thisEditMenu.add_command(label="Copy", command=self.__copy)
self.__thisEditMenu.add_command(label="Paste", command=self.__paste)
self.__thisMenuBar.add_cascade(label="Edit", menu=self.__thisEditMenu)
# Create Help and Commands menus
self.__thisHelpMenu.add_command(label="About Notepad", command=self.__showAbout)
self.__thisCommandMenu.add_command(label="About Commands", command=self.__showCommand)
self.__thisMenuBar.add_cascade(label="Commands", menu=self.__thisCommandMenu)
self.__thisMenuBar.add_cascade(label="Help", menu=self.__thisHelpMenu)
# Configure menu bar and scrollbar
self.__root.config(menu=self.__thisMenuBar)
self.__thisScrollBar.pack(side=RIGHT, fill=Y)
self.__thisScrollBar.config(command=self.__thisTextArea.yview)
self.__thisTextArea.config(yscrollcommand=self.__thisScrollBar.set)
def __quitApplication(self):
self.__root.destroy()
def __showAbout(self):
showinfo("About Notepad", "Simple text editor created using Python Tkinter")
def __showCommand(self):
showinfo("Commands", "File: New, Open, Save, Exit\nEdit: Cut, Copy, Paste")
def __openFile(self):
self.__file = askopenfilename(defaultextension=".txt",
filetypes=[("All Files","*.*"),
("Text Documents","*.txt")])
if self.__file == "":
self.__file = None
else:
self.__root.title(os.path.basename(self.__file) + " - Notepad")
self.__thisTextArea.delete(1.0, END)
with open(self.__file, "r") as file:
self.__thisTextArea.insert(1.0, file.read())
def __newFile(self):
self.__root.title("Untitled - Notepad")
self.__file = None
self.__thisTextArea.delete(1.0, END)
def __saveFile(self):
if self.__file == None:
self.__file = asksaveasfilename(initialfile='Untitled.txt',
defaultextension=".txt",
filetypes=[("All Files","*.*"),
("Text Documents","*.txt")])
if self.__file != "":
with open(self.__file, "w") as file:
file.write(self.__thisTextArea.get(1.0, END))
self.__root.title(os.path.basename(self.__file) + " - Notepad")
def __cut(self):
self.__thisTextArea.event_generate("<<Cut>>")
def __copy(self):
self.__thisTextArea.event_generate("<<Copy>>")
def __paste(self):
self.__thisTextArea.event_generate("<<Paste>>")
def run(self):
self.__root.mainloop()
# Create and run the notepad application
if __name__ == "__main__":
notepad = Notepad(width=600, height=400)
notepad.run()
Features of the Notepad
ConclusionThis Tkinter notepad provides essential text editing functionality with a clean, user-friendly interface. The application demonstrates key GUI concepts including menus, file dialogs, and event handling. You can extend this foundation by adding features like find/replace, font selection, or syntax highlighting. Updated on: 2026-03-25T05:22:30+05:30
4K+ Views Learn More in Our TutorialsAdvertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |