How to Make Notepad by Using Python Tkinter

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.

How to make notepad by using python tkinter
How to make notepad by using python tkinter

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.

90 thoughts on “How to Make Notepad by Using Python Tkinter”

  1. 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

    Reply
  2. 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

    Reply
  3. 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!!

    Reply
  4. Magnificent beat I would like to apprentice while you amend your site how can i subscribe for a blog web site The account helped me a acceptable deal I had been a little bit acquainted of this your broadcast offered bright clear idea

    Reply
  5. Good day! Do you know if they make any plugins to
    assist with SEO? I’m trying to get my website
    to rank for some targeted keywords but I’m not seeing very
    good results. If you know of any please share. Thanks!
    I saw similar blog here: Eco blankets

    Reply
  6. sugar defender official website
    As a person that’s always been cautious regarding my blood sugar level, finding Sugar Defender
    has been a relief. I really feel so much a lot more
    in control, and my current check-ups have actually revealed positive renovations.
    Understanding I have a reputable supplement to sustain my routine
    gives me satisfaction. I’m so grateful for Sugar Protector’s influence on my health
    and wellness!

    Reply
  7. The next time I learn a weblog, I hope that it doesnt disappoint me as much as this one. I imply, I do know it was my choice to learn, but I truly thought youd have one thing interesting to say. All I hear is a bunch of whining about one thing that you would repair in the event you werent too busy on the lookout for attention.

    Reply
  8. It is unusual for me to find something on the net thats as entertaining and fascinating as what youve got here. Your page is lovely, your graphics are great, and whats more, you use reference that are relevant to what you are talking about. You are certainly one in a million, man!

    Reply
  9. I hope you never stop! This is one of the best blogs Ive ever read. Youve got some mad skill here, man. I just hope that you dont lose your style because youre definitely one of the coolest bloggers out there. Please keep it up because the internet needs someone like you spreading the word.

    Reply
  10. This will be the proper weblog if you really wants to check out this topic. You are aware of much its virtually challenging to argue along with you (not too I just would want…HaHa). You certainly put a different spin on the topic thats been written about for a long time. Excellent stuff, just wonderful!

    Reply
  11. Excellent read, I simply passed this onto a colleague who has been conducting a little research on that. And the man actually bought me lunch because I discovered it for him smile So i want to rephrase that: Appreciate your lunch!

    Reply
  12. Very good blog post! Thought about valued all the looking at. I hope to find out alot more of your stuff. I presume you might fantastic information and furthermore prospect. I am just exceptionally satisfied utilizing this type of critical information.

    Reply
  13. Hello, very helpful posting. My sister and I have recently been looking to find detailed facts on this subject type of stuff for a while, yet we could hardly until now. Do you think you can also make a few youtube video clips about this, I do think your webblog would be far more complete in case you did. In any other case, oh well. I am going to be checking out on this web-site within the not too distant future. E-mail me to maintain me up-to-date. granite countertops cleveland

    Reply
  14. When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I receive four emails with the exact same comment. Can there be in whatever way you possibly can remove me from that service? Thanks!

    Reply
  15. With havin so much content and articles do you ever run into any issues of plagorism or copyright violation? My site has a lot of exclusive content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the web without my permission. Do you know any solutions to help protect against content from being ripped off? I’d certainly appreciate it.

    Reply
  16. Empathetic for your monstrous inspect, in addition I’m just seriously good as an alternative to Zune, and consequently optimism them, together with the very good critical reviews some other players have documented, will let you determine whether it does not take right choice for you.

    Reply
  17. Thank you for the good critique. Me & my cousin were just preparing to do a little research about this. We got a book from our area library but I think I’ve learned more from this post. I’m very glad to see such fantastic info being shared freely out there..

    Reply
  18. This could be the appropriate blog for everyone who hopes to discover this topic. You understand much its virtually difficult to argue together with you (not that When i would want…HaHa). You certainly put a different spin on the topic thats been discussing for several years. Excellent stuff, just fantastic!

    Reply

Leave a Comment