from tkinter import *
import tkinter.messagebox as tmsg
import os,sys
from tkinter.filedialog import askopenfilename,asksaveasfilename,asksaveasfile
root = Tk()
def New():
global file
root.title("Untitle - Notepad")
file = None
text.delete(1.0,END)
def openfile():
global file
file = askopenfilename(defaultextension = ".txt",filetypes=[("All files","*.*"),("Text Documents","*.txt")])
if file == "":
file = None
else:
root.title(os.path.basename(file) + " - Notepad")
text.delete(1.0,END)
f = open(file,"r")
text.insert(1.0,f.read())
f.close()
def Save():
global file
if file== None:
file= asksaveasfilename(initialfile='Untitled.txt',filetypes=[("All files","*.*"),("Text Documents",".txt")])
if file == "":
file = None
else:
f = open(file,"w")
f.write(text.get(1.0,END))
f.close()
root.title(os.path.basename(file) + " - Notepad")
else:
f = open(file, "r")
f.write(text.get(1.0, END))
f.close()
def Saveas():
global file
files=[("All Files","*.*"),
("Python Files","*.py"),
("Text Document","*.txt")]
file = asksaveasfile(filetypes = files, defaultextension = files)
def exit():
root.destroy()
def about():
tmsg.showinfo("help", "How can help you")
def cut():
text.event_generate(("<<Cut>>"))
def copy():
text.event_generate(("<<Copy>>"))
def past():
text.event_generate(("<<Paste>>"))
if __name__ == '__main__':
root.title("Untitle - Notepad")
root.geometry("400x400")
scrollbar=Scrollbar()
scrollbar.pack(side=RIGHT,fill=Y)
var = StringVar()
text=Text(root,yscrollcommand=scrollbar.set,font="lucida 12 bold")
file = None
text.pack(expand=True,fill=BOTH)
scrollbar.config(command=text.yview)
mainmenu=Menu(root)
m1=Menu(mainmenu,tearoff=0)
m1.add_command(label="New",command=New)
m1.add_command(label="Open",command=openfile)
m1.add_command(label="Save",command=Save)
m1.add_command(label="Save As",command=Saveas)
m1.add_command(label="Exit",command=exit)
root.config(menu=mainmenu)
mainmenu.add_cascade(label="File",menu=m1)
# copy cut
m2=Menu(mainmenu,tearoff=0)
m2.add_command(label="Cut",command=cut)
m2.add_command(label="Copy",command=copy)
m2.add_command(label="Past",command=past)
root.config(menu=mainmenu)
mainmenu.add_cascade(label="Edit",menu=m2)
#for help in this menu
m3=Menu(mainmenu,tearoff=0)
m3.add_command(label="About As",command=about)
root.config(menu=mainmenu)
mainmenu.add_cascade(label="Help",menu=m3)
root.mainloop()
Post a Comment
If you have any doubts. Please Let me Know