Creating Notepad in Tkinter Python

 


from tkinter import *
import tkinter.messagebox as tmsg
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os

root = Tk()  
root.title("Notepad")
root.geometry("1200x600")
root.wm_iconbitmap("Notepad.ico")

def new_file():
    global file
    root.title("Untitled - Notepad")
    file = None
    textwd.delete(1.0, END)
   
def open_file():
    global file
    file = askopenfilename(defaultextension=".txt", filetypes=[("All Types", "*.*"), ("Text Documents", "*.txt")])
    if file == "":
        file = None
    else:
        root.title(os.path.basename(file) + " - Notepad")
        textwd.delete(1.0, END)
        f = open(file, "r")
        textwd.insert(1.0, f.read())
        f.close()
       
def save_file():
    global file
    if file == None:
        file = asksaveasfilename(initialfile="Untitled.txt", filetypes=[("All Types", "*.*"), ("Text Documents", "*.txt")])
        if file == "":
            file = None
        else:
            f = open(file, "w")
            f.write(textwd.get(1.0, END))
            f.close()
           
            root.title(os.path.basename(file) + " - Notepad")
    else:
        f = open(file, "w")
        f.write(textwd.get(1.0, END))
        f.close()
       
def cut():
    textwd.event_generate(("<<Cut>>"))

def copy():
    textwd.event_generate(("<<Copy>>"))

def paste():
    textwd.event_generate(("<<Paste>>"))
   
def exitnp():
    root.destroy()

def get_help():
    tmsg.showinfo("Help from Notepad", "This is Abdur Rehman. I will help you as far as possible here.")
   
def about():
    tmsg.showinfo("Who created this Notepad?", "This Notepad is created by ABDUR REHMAN")
   
def feedback():
    a = tmsg.askquestion("Rate this Notepad", "Is this notepad useful for you guys?")
    if a == "yes":
        tmsg.showinfo("Thank You!", "Very well! WE will work more for its improvement")
    else:
        tmsg.showinfo("Ooooops!", "Sorry to hear that. WE will work more on this notepad project to improve its functionality.")
   
def bing():
    pass

# menu line in the top-left
main_menu = Menu(root)

# submenu 1
m1 = Menu(main_menu, tearoff=0)
m1.add_command(label="New", command=new_file)
m1.add_command(label="New Window")
m1.add_command(label="Open", command=open_file)
m1.add_command(label="Save", command=save_file)
m1.add_command(label="Save As",)
m1.add_separator()
m1.add_command(label="Page Setup")
m1.add_command(label="Print")
m1.add_separator()
m1.add_command(label="Exit", command=exitnp)
main_menu.add_cascade(label="File", menu=m1)

# submenu 2
m2 = Menu(main_menu, tearoff=0)
m2.add_command(label="Undo")
m2.add_separator()
m2.add_command(label="Cut", command=cut)
m2.add_command(label="Copy", command=copy)
m2.add_command(label="Paste", command=paste)
m2.add_command(label="Delete")
m2.add_separator()
m2.add_command(label="Search with Bing", command=bing)
m2.add_command(label="Find...")
m2.add_command(label="Find Next")
m2.add_command(label="Find Previous")
m2.add_command(label="Replace...")
m2.add_command(label="Go To")
m2.add_separator()
m2.add_command(label="Select All")
m2.add_command(label="Time/Date")
main_menu.add_cascade(label="Edit", menu=m2)

# submenu 3
m3 = Menu(main_menu, tearoff=0)
m3.add_command(label="Word Wrap")
m3.add_command(label="Font...")
main_menu.add_cascade(label="Format", menu=m3)

# submenu 4
m4 = Menu(main_menu, tearoff=0)
m4.add_command(label="Zoom >")
m4.add_command(label="Status Bar")
main_menu.add_cascade(label="View", menu=m4)

# submenu 5
m4 = Menu(main_menu, tearoff=0)
m4.add_command(label="View Help", command=get_help)
m4.add_command(label="Send Feedback", command=feedback)
m4.add_separator()
m4.add_command(label="About Notepad", command=about)
main_menu.add_cascade(label="Help", menu=m4)

root.config(menu=main_menu)

myscroll = Scrollbar(root)
myscroll.pack(side=RIGHT, fill=Y)

textwd = Text(root, font="verdana 22", bg="white", fg="black", yscrollcommand=myscroll.set)
file = None
textwd.pack()

myscroll.config(command=textwd.yview)

root.mainloop()





Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

What is the difference between iostream and iostream.h in cpp?

The Basic Structure of a Full-Stack Web App