Coders Packet

Creating notepad using Tkinter GUI in Python

By Ayush Kumar JHA

This is a simple notepad that being created in Python using Tkinter library, which helps us to SAVE, CLEAR, and OPEN our files in notepad

output of notepad codeFirst, we will start by making the interface of our Tkinter, by importing essential libraries,  setting the padding, border and desired background colour.

from tkinter import *
from tkinter import filedialog

root=Tk()
root.geometry("500x600")
root.config(bg="gray")
frame = Frame(root)
frame.pack()

 

After that, in the second process we will be designing buttons for our operation that notepad will do for us, like:

->SAVE: this function let us save the file we wrote on the notepad

->CLEAR: this function let us clear the whole page.

->OPEN: this function will let us to open the file which is already stored in our file manager.

b1=Button(root,  text="SAVE", command=save_file, bg="orange")
b1.place(x=10, y=10)

b2=Button(root,  text="CLEAR", command=clear_file, bg="white")
b2.place(x=70, y=10)

b3=Button(root,  text="OPEN", command=open_file, bg="green")
b3.place(x=150, y=10)

entry=Text(root, height=33, width=58, wrap=WORD)
entry.place(x=10, y=50)

 

 

Now, when we are ready with all our commanding buttons and everything, we will enable these buttons by giving them commands to do their assigned work.

def save_file():
    open_file=filedialog.asksaveasfile(mode='w',  defaultextension=".txt") 
    if open_file is None:
         return
    text=str(entry.get(1.0 , END))
    open_file.write(text)
    open_file.close()
    
    
def clear_file():
    entry.delete(1.0, END)
    
def open_file():
    file=filedialog.askopenfile(mode='r', filetype=[('text files', '*.text')])
    if file is not None:
        content=file.read()
    entry.insert(INSERT, content )
    

 

Download project

Reviews Report

Submitted by Ayush Kumar JHA (akjayush)

Download packets of source code on Coders Packet