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
I really like your writing style, wonderful info, thanks for putting up :D. “Kennedy cooked the soup that Johnson had to eat.” by Konrad Adenauer.
Wow wonderful blog layout How long have you been blogging for you make blogging look easy The overall look of your site is great as well as the content
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Wonderful beat I wish to apprentice while you amend your web site how could i subscribe for a blog web site The account aided me a acceptable deal I had been a little bit acquainted of this your broadcast provided bright clear idea
Yaşamkent diyetisyen, sağlıklı yaşam ve dengeli beslenme arayışında olan birçok kişi için önemli bir merkez haline gelmiştir.
Site fantástico Muitas informações úteis aqui estou enviando para alguns amigos e também compartilhando deliciosos E claro obrigado pelo seu esforço
Site fantástico Muitas informações úteis aqui, estou enviando para alguns amigos e também compartilhando deliciosas E, claro, obrigado pelo seu suor
Real Estate Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated
Viyana Teknik Üniversitesi, mühendislik, bilgi teknolojisi, doğa bilimleri ve daha birçok alanda sunduğu geniş kapsamlı programlarla dikkat çeker.
Techno rozen I really like reading through a post that can make men and women think. Also, thank you for allowing me to comment!
Mygreat learning naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
BaddieHub I am truly thankful to the owner of this web site who has shared this fantastic piece of writing at at this place.
Simplywall I very delighted to find this internet site on bing, just what I was searching for as well saved to fav
Normally I do not read article on blogs however I would like to say that this writeup very forced me to try and do so Your writing style has been amazed me Thanks quite great post
Simply Sseven I appreciate you sharing this blog post. Thanks Again. Cool.
Simply Sseven Good post! We will be linking to this particularly great post on our site. Keep up the great writing
Somebody essentially lend a hand to make significantly posts I might state That is the very first time I frequented your web page and up to now I surprised with the research you made to create this particular put up amazing Excellent job
Зарегистрируйтесь прямо сейчас и получите 100 фриспинов без депозита, чтобы испытать свою удачу в увлекательных играх и повысить свои шансы на крупный выигрыш. рейтинг казино онлайн qjfnqievws …
Hi, i think that i saw you visited my web site thus i came to ?eturn the favor텶’m attempting to find things to enhance my site!I suppose its ok to use a few of your ideas!!
Bu soba, içindeki yakıtın yanmasıyla oluşan ısıyı doğrudan çevresine yayar ve aynı zamanda suyun ısınmasını sağlar.
Pink Withney I just like the helpful information you provide in your articles