Simple Note pad python with tkinter


Simple Note pad python with tkinter

overall a working notepad




 This is a simple note pad where we can  write any note and  save it on the required folder

 It provide following applications:-

  • New- for creating new file
  • open - for opening previous text file
  • exit- to exit note pad
  • copy- to copy selected text
  • paste - to paste copied text
  • cut- to cut selected text
  • Save - for saving existing note


Screen short of notepad





import tkinter as tk
from tkinter import*
from tkinter import messageboxfiledialog
import os
from genericpath import samefile

def createWidgets():

    global textArea
    textArea = Text(root)
    textArea.grid(sticky=N+E+S+W)

    menuBar = Menu(root)
    root.config(menu=menuBar)

    # for dropdown file menue
    fileMenu = Menu(menuBartearoff=0)
    fileMenu.add_command(label="New"command=newFile)
    fileMenu.add_command(label="Open"command=openFile)
    fileMenu.add_command(label="Save"command=saveFile)
    fileMenu.add_separator()
    fileMenu.add_command(label="Exit"command=exitfile)
    menuBar.add_cascade(label="file"menu=fileMenu)

    # for dropdown edit menue
    editMenu = Menu(menuBartearoff=0)
    editMenu.add_command(label="Copy"command=copy)
    editMenu.add_command(label="Cut"command=cut)
    editMenu.add_command(label="Paste"command=paste)
    menuBar.add_cascade(label="Edit"menu=editMenu)
    
    # for dropdown help menue
    helpMenu = Menu(menuBartearoff=0)
    helpMenu.add_command(label="About Notepad"command=about)
    menuBar.add_cascade(label="Help"menu=helpMenu)


def about():
    messagebox.showinfo(
        "Notpad""This simple notepad is devolped by Qamar and integrated by Sandeep")


def newFile():
    global textArea
    root.title("Untitle -Notepad")
    file = None
    textArea.delete(1.0END)


def openFile():
    global textArea
    file = filedialog.askopenfile(defaultextension=".txt"filetypes=[
                                  ("All files""*.*"), ("text Document""*.txt")])
    file = file.name

    if file == "":
        file = None
    else:
        root.title(os.path.basename(file)+" - Notepad")
        textArea.delete(1.0END)
        file = open(file"rb")
        textArea.insert(1.0file.read())
        file.close()


def saveFile():
    global textAreafile
    if file == None:
        file = filedialog.asksaveasfilename(initialfile="Untitled.txt"defaultextension=".txt"filetypes=[
                                            ("All files""*.*"), ("text Document""*.txt")])

        if file == None:
            file = None
        else:
            file = open(file"w")
            file.write(textArea.get(1.0END))
            file.close()
            file.name
            root.title(os.path.basename(file) + " -Notepad")
    else:
        file = open(file"w")
        file.write(textArea.get(1.0END))
        file.close()


def exitfile():
    root.destroy()


def cut():
    global textArea
    textArea.event_generate("<<Cut>>")


def copy():
    global textArea
    textArea.event_generate("<<Copy>>")


def paste():
    global textArea
    textArea.event_generate("<<Paste>>")


root = tk.Tk()
root.title("untitle - notepad")
file = None
createWidgets()
root.mainloop()

Comments