In this Python tkinter tutorial, how to make notepad by using Python Tkinter and we will also cover the different examples related to the Python tkinter. Besides this, we will also cover the whole code related to How to make notepad by using python tkinter.
How to Make Notepad by Using Python Tkinter
Notepad is used as a simple text editor it can build and corrects the plain text documents. The notepad is also known as a sheet of a paper that are used for written the things.
Github Link
Check this code in Repository from Github and you can also fork this code.
Github User Name: PythonT-Point
Block of Code:
In this block of code we importing the tkinter library such as import tkinter, import os, from tkinter import *, import tkinter.messagebox import *, from tkinter.filedialog import *.
# How to make notepad by using Python Tkinter
import tkinter
import os
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
Block of Code:
In this block of code we are creating the window and also giving the width and height to the window and then creating the text area and menu bar and after that we are creating the menu bar .
root = Tk()
# default window width and height
width = 600
height = 400
textarea = Text(root)
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
editmenu = Menu(menubar, tearoff=0)
helpmenu = Menu(menubar, tearoff=0)
Block of Code:
In this block of code we are adding the scrollbar by using scrollbar() method and also defining the init function.
# To add scrollbar
scrollbar = Scrollbar(textarea)
file = None
def __init__(self,**kwargs):
# Set icon
try:
self.root.wm_iconbitmap("Notepad.ico")
except:
pass
Block of code:
In this block of code we will set the window size and then we will set the window text and after that we are creating the window and then we are making the text area resizeable and after that creating a feature of description of the notepad after that adjust Scrollbar automatically according to the content.
# Set window size
try:
self.width = kwargs['width']
except KeyError:
pass
try:
self.height = kwargs['height']
except KeyError:
pass
# Set the window text
self.root.title("Untitled - Notepad")
# Center the window
screenWidth = self.root.winfo_screenwidth()
screenHeight = self.root.winfo_screenheight()
# For left-align
left = (screenWidth / 2) - (self.width / 2)
# For right-align
top = (screenHeight / 2) - (self.height /2)
# For top and bottom
self.root.geometry('%dx%d+%d+%d' % (self.width,
self.height,
left, top))
# To make the textarea auto resizable
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
# Add controls (widget)
self.textarea.grid(sticky = N + E + S + W)
# To open new file
self.filemenu.add_command(label="New",
command=self.newfile)
# To open a already existing file
self.filemenu.add_command(label="Open",
command=self.openfile)
# To save current file
self.filemenu.add_command(label="Save",
command=self.savefile)
# To create a line in the dialog
self.filemenu.add_separator()
self.filemenu.add_command(label="Exit",
command=self.quitapplication)
self.menubar.add_cascade(label="File",
menu=self.filemenu)
# To give a feature of cut
self.editmenu.add_command(label="Cut",
command=self.cut)
# to give a feature of copy
self.editmenu.add_command(label="Copy",
command=self.copy)
# To give a feature of paste
self.editmenu.add_command(label="Paste",
command=self.paste)
# To give a feature of editing
self.menubar.add_cascade(label="Edit",
menu=self.editmenu)
# To create a feature of description of the notepad
self.helpmenu.add_command(label="About Notepad",
command=self.showabout)
self.menubar.add_cascade(label="Help",
menu=self.helpmenu)
self.root.config(menu=self.menubar)
self.scrollbar.pack(side=RIGHT,fill=Y)
# Scrollbar will adjust automatically according to the content
self.scrollbar.config(command=self.textarea.yview)
self.textarea.config(yscrollcommand=self.scrollbar.set)
Block of code:
In this block of code we will defining the quit application and then defining the show about after that defining the open file.
def quitapplication(self):
self.root.destroy()
# exit()
def showabout(self):
showinfo("Notepad","Mrinal Verma")
def openfile(self):
self.file = askopenfilename(defaultextension=".txt",
filetypes=[("All Files","*.*"),
("Text Documents","*.txt")])
if self.file == "":
# no file to open
self.file = None
else:
# Try to open the file
# set the window title
self.root.title(os.path.basename(self.file) + " - Notepad")
self.textarea.delete(1.0,END)
file = open(self.file,"r")
self.textarea.insert(1.0,file.read())
file.close()
Block of code:
In this block of code we will defining the new file function and then we will defining the save file function and after that defining cut, copy, paste, and run function.
def newfile(self):
self.root.title("Untitled - Notepad")
self.file = None
self.textarea.delete(1.0,END)
def savefile(self):
if self.file == None:
# Save as new file
self.file = asksaveasfilename(initialfile='Untitled.txt',
defaultextension=".txt",
filetypes=[("All Files","*.*"),
("Text Documents","*.txt")])
if self.file == "":
self.file = None
else:
# Try to save the file
file = open(self.file,"w")
file.write(self.textarea.get(1.0,END))
file.close()
# Change the window title
self.root.title(os.path.basename(self.file) + " - Notepad")
else:
file = open(self.file,"w")
file.write(self.textarea.get(1.0,END))
file.close()
def cut(self):
self.textarea.event_generate("<<Cut>>")
def copy(self):
self.textarea.event_generate("<<Copy>>")
def paste(self):
self.textarea.event_generate("<<Paste>>")
def run(self):
# Run main application
self.root.mainloop()
# Run main application
notepad = Notepad(width=600,height=400)
notepad.run()
Code:
Hereafter splitting the code and explaining how to make Notepad by using in Python Tkinter, now we will see how the output look like after running the whole code.
# How to make notepad by using Python Tkinter
# How to make notepad by using Python Tkinter
import tkinter
import os
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
class Notepad:
root = Tk()
# default window width and height
width = 600
height = 400
textarea = Text(root)
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
editmenu = Menu(menubar, tearoff=0)
helpmenu = Menu(menubar, tearoff=0)
# To add scrollbar
scrollbar = Scrollbar(textarea)
file = None
def __init__(self,**kwargs):
# Set icon
try:
self.root.wm_iconbitmap("Notepad.ico")
except:
pass
# Set window size
try:
self.width = kwargs['width']
except KeyError:
pass
try:
self.height = kwargs['height']
except KeyError:
pass
# Set the window text
self.root.title("Untitled - Notepad")
# Center the window
screenWidth = self.root.winfo_screenwidth()
screenHeight = self.root.winfo_screenheight()
# For left-align
left = (screenWidth / 2) - (self.width / 2)
# For right-align
top = (screenHeight / 2) - (self.height /2)
# For top and bottom
self.root.geometry('%dx%d+%d+%d' % (self.width,
self.height,
left, top))
# To make the textarea auto resizable
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
# Add controls (widget)
self.textarea.grid(sticky = N + E + S + W)
# To open new file
self.filemenu.add_command(label="New",
command=self.newfile)
# To open a already existing file
self.filemenu.add_command(label="Open",
command=self.openfile)
# To save current file
self.filemenu.add_command(label="Save",
command=self.savefile)
# To create a line in the dialog
self.filemenu.add_separator()
self.filemenu.add_command(label="Exit",
command=self.quitapplication)
self.menubar.add_cascade(label="File",
menu=self.filemenu)
# To give a feature of cut
self.editmenu.add_command(label="Cut",
command=self.cut)
# to give a feature of copy
self.editmenu.add_command(label="Copy",
command=self.copy)
# To give a feature of paste
self.editmenu.add_command(label="Paste",
command=self.paste)
# To give a feature of editing
self.menubar.add_cascade(label="Edit",
menu=self.editmenu)
# To create a feature of description of the notepad
self.helpmenu.add_command(label="About Notepad",
command=self.showabout)
self.menubar.add_cascade(label="Help",
menu=self.helpmenu)
self.root.config(menu=self.menubar)
self.scrollbar.pack(side=RIGHT,fill=Y)
# Scrollbar will adjust automatically according to the content
self.scrollbar.config(command=self.textarea.yview)
self.textarea.config(yscrollcommand=self.scrollbar.set)
def quitapplication(self):
self.root.destroy()
# exit()
def showabout(self):
showinfo("Notepad","Mrinal Verma")
def openfile(self):
self.file = askopenfilename(defaultextension=".txt",
filetypes=[("All Files","*.*"),
("Text Documents","*.txt")])
if self.file == "":
# no file to open
self.file = None
else:
# Try to open the file
# set the window title
self.root.title(os.path.basename(self.file) + " - Notepad")
self.textarea.delete(1.0,END)
file = open(self.file,"r")
self.textarea.insert(1.0,file.read())
file.close()
def newfile(self):
self.root.title("Untitled - Notepad")
self.file = None
self.textarea.delete(1.0,END)
def savefile(self):
if self.file == None:
# Save as new file
self.file = asksaveasfilename(initialfile='Untitled.txt',
defaultextension=".txt",
filetypes=[("All Files","*.*"),
("Text Documents","*.txt")])
if self.file == "":
self.file = None
else:
# Try to save the file
file = open(self.file,"w")
file.write(self.textarea.get(1.0,END))
file.close()
# Change the window title
self.root.title(os.path.basename(self.file) + " - Notepad")
else:
file = open(self.file,"w")
file.write(self.textarea.get(1.0,END))
file.close()
def cut(self):
self.textarea.event_generate("<<Cut>>")
def copy(self):
self.textarea.event_generate("<<Copy>>")
def paste(self):
self.textarea.event_generate("<<Paste>>")
def run(self):
# Run main application
self.root.mainloop()
# Run main application
notepad = Notepad(width=600,height=400)
notepad.run()
Output:
After running the whole code we get the following output in which we can see that the notepad is created on the screen with the help of Python Tkinter on the screen.

So, in this tutorial, we have illustrated How to make notepad by using Python Tkinter. Moreover, we have also discussed the whole code used in this tutorial.
Read some more tutorials related to Python Turtle.
- Python Turtle Snake Game
- Python Turtle Chess Game
- How to Make a Dog Face Using Python Turtle
- How to Write Happy Birthday using Python Turtle
- How to Draw Netflix Logo in Python Turtle
