Home » Tutorials » How to Create a Notepad using Python Tkinter

How to Create a Notepad using Python Tkinter

In today’s tutorial, we’re diving into a fun and practical project: creating a Notepad using Python and Tkinter. Imagine having your own custom text editor with a user-friendly interface, where you can easily open, save, and edit your text files. Sounds cool, right? We’ll break it down step-by-step, making it super easy to follow along, even if you’re a beginner. By the end of this tutorial, you’ll have built a simple yet powerful Notepad application, and you’ll have learned some valuable programming skills along the way.

Let’s get started and bring this handy tool to life!

Table of Contents

Necessary Libraries

For this code to work as intended, make sure to install the tkinter library. via the terminal or your command prompt and type in the command.

$ pip install tk

This library allows the creation of a graphical user interface.

Imports

from tkinter import *
from tkinter import filedialog
from tkinter import scrolledt

We start by importing everything from tkinter to create the main window for our application, next, we import filedialog which is used for file opening and saving dialogs. Finally, we import scrolledtext which is used to create a text editor with a scrollable area.

Create the Main Window

#creating the main window
root = Tk()
root.title("NotePad - The Pycodes")
root.geometry("600x600")
root.config(bg='lightblue')

After that, we create the main window:

  • The first line creates the main application window.
  • The second sets the title of the window.
  • The third sets the initial size of the window to 600×600 pixels.
  • The last sets the background color of the window to light blue.

Defining Functions

Then we create the save_file() function that opens a filedialog to save the text content in the text editor.

#defining functions
def save_file():
    file = filedialog.asksaveasfile(defaultextension='.txt')
    if file:
        text = text_editor.get(1.0,END)
        file.write(text)
        file.close()

Next, we define the open_file() function which opens a filedialog to open an existing text file and displays its content in the text editor.

def open_file():
    file = filedialog.askopenfile(filetype=[('text files','*.txt')])
    if file:
        content = file.read()
        text_editor.delete(1.0,END)
        text_editor.insert(INSERT,content)

Creating the Menu bar

#creating menu bar
menu_bar = Menu(root)
root.config(menu=menu_bar)

Now, we create a menu bar at the top of the window and set the menu bar as the main menu for the application.

Creating a File Menu with Options

#creating a file menu with options
file_menu = Menu(menu_bar)
menu_bar.add_cascade(label="File",menu=file_menu)
file_menu.add_command(label="Open File",command=open_file)
file_menu.add_command(label="Save File",command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit",command=root.quit)

For this step, we created a submenu under the main menu then we added a “File” menu to the menu bar and inside the “File” menu, we put the options that are  “Open File” “Save File” And “Exit“.

Adding a Text Editor Area

#adding a text editor area
text_editor = scrolledtext.ScrolledText(root,wrap=WORD,width=60,height=50)
text_editor.pack(pady=20)

Following that, we create a text editor area with word wrapping, a width of 60 characters, and a height of 50 lines, and add the text editor to the main window with some padding.

Running the GUI Application

#running the GUI application
root.mainloop()

Lastly, this line starts the Tkinter event loop, which keeps the GUI application running and responsive to user interactions.

Example

Full Code

from tkinter import *
from tkinter import filedialog
from tkinter import scrolledtext


#creating the main window
root = Tk()
root.title("NotePad - The Pycodes")
root.geometry("600x600")
root.config(bg='lightblue')


#defining functions
def save_file():
    file = filedialog.asksaveasfile(defaultextension='.txt')
    if file:
        text = text_editor.get(1.0,END)
        file.write(text)
        file.close()


def open_file():
    file = filedialog.askopenfile(filetype=[('text files','*.txt')])
    if file:
        content = file.read()
        text_editor.delete(1.0,END)
        text_editor.insert(INSERT,content)


#creating menu bar
menu_bar = Menu(root)
root.config(menu=menu_bar)


#creating a file menu with options
file_menu = Menu(menu_bar)
menu_bar.add_cascade(label="File",menu=file_menu)
file_menu.add_command(label="Open File",command=open_file)
file_menu.add_command(label="Save File",command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit",command=root.quit)


#adding a text editor area
text_editor = scrolledtext.ScrolledText(root,wrap=WORD,width=60,height=50)
text_editor.pack(pady=20)


#running the GUI application
root.mainloop()

Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top