0% found this document useful (0 votes)
4 views10 pages

Python Project Notepad Source Code-1

The document outlines a simple text editor application built using Python and Tkinter, featuring a Model-View-Controller (MVC) architecture. The Model class handles file encryption and decryption, while the Controller class manages user interactions and file operations. The View class creates the graphical user interface, including menus, toolbars, and text editing functionalities.

Uploaded by

ma4230907
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Python Project Notepad Source Code-1

The document outlines a simple text editor application built using Python and Tkinter, featuring a Model-View-Controller (MVC) architecture. The Model class handles file encryption and decryption, while the Controller class manages user interactions and file operations. The View class creates the graphical user interface, including menus, toolbars, and text editing functionalities.

Uploaded by

ma4230907
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Model Class

import [Link]
import speech_recognition as s

class Model:
def __init__(self):
[Link] =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
[Link] = 5

def encrypt(self,plaintext):
result=''
for ch in plaintext :
try:
i=([Link](ch)+[Link])%62
result+=[Link][i]
except ValueError:
result+=ch
return result

def decrypt(self, ciphertext):


result = ''
for ch in ciphertext:
try:
i = ([Link](ch) - [Link])%62
result += [Link][i]
except ValueError:
result += ch
return result

def save_file(self,msg,url):
if type(url) is not str:
file=[Link]
else:
file=url
filename,file_extension=[Link](file)
if file_extension in '.ntxt':
msg=[Link](msg)
with open(file,"w")as f:
[Link](msg)

def read_file(self,url):
base=[Link](url)
filename,file_extension=[Link](url)
with open(url,"r")as fr:
msg=[Link]()
if file_extension in '.ntxt':
msg=[Link](msg)
return msg,base

def save_as(self,msg,url):
if type(url) is not str:
file=[Link]
else:
file=url
msg=[Link](msg)
with open(file,"w")as fw:
[Link](msg)
def takeQuery(self):
sr = [Link]()
sr.pause_threshold = 1
print("speak")
with [Link]() as m:
try:
# sr.adjust_for_ambient_noise(m)
audio = [Link](m)
query = sr.recognize_google(audio, language='eng-in') # google
api client library
return query
except Exception as e:
print("exception in this", e)
print("not recognized")

Control Class

import NotePadModel

class Controller:
def __init__(self):
[Link]=[Link]()

def save_file(self,msg,url):
[Link].save_file(msg,url)

def save_as(self, msg, url):


[Link].save_as(msg,url)

def read_file(self, url):


[Link],[Link]= [Link].read_file(url)
return [Link],[Link]

def saySomeThing(self):
[Link]=[Link]()
return [Link]

View class

import NotePadController
import tkinter as tk
import traceback
from tkinter import ttk
from tkinter import font,colorchooser,messagebox,filedialog

class Notepad:
def __init__(self,root):
[Link]=root
[Link]=''
self.text_changed=False
self.show_statusbar=[Link]()
self.show_statusbar.set(True)
self.show_toolbar=[Link]()
self.show_toolbar.set(True)
[Link]=[Link]()
[Link]('1360x700+0+0')
[Link]('MyNotePad')
[Link].wm_iconbitmap('[Link]')
self.set_icons()
self.set_menu_bar()
self.set_file_sub_menu()
self.set_view_sub_menu()
self.set_color_sub_menu()
self.set_edit_sub_menu()
self.set_tool_bar()
self.set_tool_bar_event_bindings()
self.set_canvas()
self.set_status_bar()
self.set_file_menu_event_bindings()
[Link]("WM_DELETE_WINDOW",self.exit_func)

def set_icons(self):
self.new_icon=[Link](file='icons/[Link]')
self.open_icon = [Link](file='icons/[Link]')
self.save_icon = [Link](file='icons/[Link]')
self.save_as_icon = [Link](file='icons/save_as.png')
self.exit_icon = [Link](file='icons/[Link]')
self.copy_icon=[Link](file='icons/[Link]')
self.paste_icon = [Link](file='icons/[Link]')
self.cut_icon = [Link](file='icons/[Link]')
self.clear_all_icon = [Link](file='icons/clear_all.png')
self.find_icon = [Link](file='icons/[Link]')
self.tool_bar_icon = [Link](file='icons/tool_bar.png')
self.status_bar_icon = [Link](file='icons/status_bar.png')
self.light_default_icon = [Link](file='icons/light_default.png')
self.light_plus_icon = [Link](file='icons/light_plus.png')
self.dark_icon = [Link](file='icons/[Link]')
self.red_icon = [Link](file='icons/[Link]')
self.monokai_icon = [Link](file='icons/[Link]')
self.night_blue_icon = [Link](file='icons/night_blue.png')

def set_menu_bar(self):
self.main_menu=[Link]()
[Link]=[Link](self.main_menu,tearoff=False)
[Link] = [Link](self.main_menu, tearoff=False)
[Link] = [Link](self.main_menu, tearoff=False)
self.color_theme = [Link](self.main_menu, tearoff=False)
self.main_menu.add_cascade(label='File',menu=[Link])
self.main_menu.add_cascade(label='Edit', menu=[Link])
self.main_menu.add_cascade(label='View', menu=[Link])
self.main_menu.add_cascade(label='Color Theme', menu=self.color_theme)
[Link](menu=self.main_menu)

def set_file_sub_menu(self):

[Link].add_command(label='New',image=self.new_icon,compound=[Link],accelerator
='Ctrl+N',command=self.new_file)
[Link].add_command(label='Open', image=self.open_icon,
compound=[Link], accelerator='Ctrl+O', command=self.open_file)
[Link].add_command(label='Save', image=self.save_icon,
compound=[Link], accelerator='Ctrl+S', command=self.save_file)
[Link].add_command(label='Save as', image=self.save_as_icon,
compound=[Link], accelerator='Alt+S',command=self.save_as)
[Link].add_command(label='Exit', image=self.exit_icon,
compound=[Link], accelerator='Ctrl+Q',command=self.exit_func)

def set_canvas(self):
self.text_editor=[Link]([Link])
self.text_editor.configure(wrap='word',relief=[Link])
self.text_editor.focus_set()
self.scroll_bar=[Link]([Link])
self.scroll_bar.pack(side=[Link],fill=tk.Y)
self.text_editor.pack(fill=[Link],expand=True)
self.scroll_bar.configure(command=self.text_editor.yview)
self.text_editor.configure(yscrollcommand=self.scroll_bar.set)
self.text_changed=False
self.text_editor.bind('<<Modified>>',[Link])

def set_status_bar(self):
self.status_bar=[Link]([Link],text="status bar")
self.status_bar.pack(side=[Link])
#[Link]=0

def set_file_menu_event_bindings(self):
[Link]("<Control-n>",self.new_file)
[Link]("<Control-N>", self.new_file)
[Link]("<Control-o>", self.open_file)
[Link]("<Control-O>", self.open_file)
[Link]("<Control-s>", self.save_file)
[Link]("<Control-S>", self.save_file)
[Link]("<Control-q>", self.exit_func)
[Link]("<Control-Q>", self.exit_func)
[Link]("<Alt-s>", self.save_as)
[Link]("<Alt-S>", self.save_as)

def save_file(self, event=None):


try:
content = self.text_editor.get(1.0, "end-1c")
if [Link] == "":
self.save_dialog()
[Link].save_file(content, [Link])
self.text_changed = False
else:
[Link].save_file(content, [Link])
self.text_changed = False
except Exception:
[Link]("Error!", "Please select a file to save")
print(traceback.format_exc())

def save_dialog(self):

[Link]=[Link](mode="w",defaultextension='.ntxt',filetypes=([("
All files","*.*"),("Text Documents","*.txt")]))

def open_file(self, event=None):


try:
self.open_dialog()
[Link], [Link] = [Link].read_file([Link])
self.text_editor.delete(1.0, [Link])
self.text_editor.insert(1.0, [Link])
[Link]([Link])
self.text_editor.edit_modified(False)
except FileNotFoundError:
[Link]("Error!", "Please select a file first")
print(traceback.format_exc())

def open_dialog(self):
[Link] = [Link](title='select File',
filetypes=[("Text Document", "*.*")])

def save_as(self, event=None):


try:
content = self.text_editor.get(1.0, "end-1c")
self.save_dialog()
[Link].save_as(content, [Link])
self.text_changed = False
except Exception:
[Link]("Error!", "Please select a file to Save as")
print(traceback.format_exc())

def new_file(self,event=None):
[Link]=''
self.text_editor.delete(1.0,[Link])
[Link]("MyNotePade")

def changed(self, event=None):


words = len(self.text_editor.get(1.0, 'end-1c').split())
char = len(self.text_editor.get(1.0, "end-1c"))
self.status_bar.configure(text=f'characters : {char} words :{words}')
if self.text_editor.edit_modified():
self.text_changed = True
self.text_editor.edit_modified(False)

def exit_func(self,event=None):
result=[Link]("App Closing","Do You Want To Quit")
if result == False:
return
try:
if [Link]=='':
if len(self.text_editor.get('1.0','end-1c'))==0:
self.text_changed=False
if self.text_changed:
mbox=[Link]("warning","Do you want to save the
file")
if mbox==True:
content=self.text_editor.get('1.0','end-1c')
if [Link]=='':
self.save_dialog()
[Link].save_file(content,[Link])
else:
[Link].save_file(content, [Link])
[Link]("Have a good day!","thank you for using")
[Link]()
except:
[Link]("Error!", "Please select a file to save")
print(traceback.format_exc())

def set_edit_sub_menu(self):

[Link].add_command(label='Copy',image=self.copy_icon,compound=[Link],accelerat
or='Ctrl+C',command=lambda :self.text_editor.event_generate("<<Copy>>"))
[Link].add_command(label='Paste', image=self.paste_icon,
compound=[Link], accelerator='Ctrl+V',command=lambda:
self.text_editor.event_generate("<<Paste>>"))
[Link].add_command(label='Cut', image=self.cut_icon, compound=[Link],
accelerator='Ctrl+X',command=lambda: self.text_editor.event_generate("<<Cut>>"))
[Link].add_command(label='Clear all', image=self.clear_all_icon,
compound=[Link], accelerator='Alt+X',command=lambda:
self.text_editor.delete(1.0,[Link]))
[Link].add_command(label='Find', image=self.find_icon,
compound=[Link], accelerator='Ctrl+F',command= self.find_func)

def find_func(self,event=None):
self.find_dialogue=[Link]()
self.find_dialogue.geometry('450x250+500+200')
self.find_dialogue.title('find')
self.find_dialogue.resizable(0,0)
self.find_frame=[Link](self.find_dialogue,text='Find/replace')
self.find_frame.pack(pady=20)
self.text_find_label=[Link](self.find_frame,text= 'Find:')
self.text_replace_label=[Link](self.find_frame,text='Replace:')
self.find_input=[Link](self.find_frame,width=30)
self.replace_input = [Link](self.find_frame, width=30)
self.find_button=[Link](self.find_frame,text='Find',command=[Link])
self.replace_button = [Link](self.find_frame, text='Replace',
command=[Link])
self.text_find_label.grid(row=0,column=0,padx=4,pady=4)
self.text_replace_label.grid(row=1, column=0, padx=4, pady=4)
self.find_input.grid(row=0, column=1, padx=4, pady=4)
self.replace_input.grid(row=1, column=1, padx=4, pady=4)
self.find_button.grid(row=2,column=0,padx=8,pady=4)
self.replace_button.grid(row=2,column=1,padx=8,pady=4)
self.find_dialogue.mainloop()

def find(self):
word=self.find_input.get()
self.text_editor.tag_remove('match','1.0',[Link])
matches=0
if word:
start_pos='1.0'
while True:
start_pos=self.text_editor.search(word,start_pos,stopindex=[Link])
if not start_pos:
break
end_pos=f'{start_pos}+{len(word)}c'
print(start_pos,end_pos)
self.text_editor.tag_add('match',start_pos,end_pos)
matches+=1
start_pos=end_pos

self.text_editor.tag_configure('match',foreground='red',background='yellow')
[Link]("word found",f'{word} occurs {matches} times')

def replace(self):
word=self.find_input.get()
replace=self.replace_input.get()
str=self.text_editor.get(1.0,[Link]).replace(word,replace)
self.text_editor.delete(1.0,[Link])
self.text_editor.insert(1.0,str)
def set_tool_bar(self):
self.tool_bar = [Link]([Link])
self.tool_bar.pack(side=[Link],fill=tk.X)
## font box
self.font_tuple = [Link]()
self.font_family = [Link]()
self.font_box = [Link](self.tool_bar, width=30,
textvariable=self.font_family,state = 'readonly')
self.font_box['values'] = self.font_tuple
self.font_box.current(self.font_tuple.index('Arial'))
self.font_box.grid(row=0, column=0,padx=5)
## size box
self.size_var = [Link]()
self.font_size = [Link](self.tool_bar, width=14,
textvariable=self.size_var, state='readonly')
self.font_size['values'] = tuple(range(8, 81))
self.font_size.current(4)
self.font_size.grid(row=0, column=1, padx=5)
## bold button
self.bold_icon = [Link](file='icons/[Link]')
self.bold_btn = [Link](self.tool_bar, image=self.bold_icon)
self.bold_btn.grid(row=0, column=2, padx=5)
## italic button
self.italic_icon = [Link](file='icons/[Link]')
self.italic_btn = [Link](self.tool_bar, image=self.italic_icon)
self.italic_btn.grid(row=0, column=3, padx=5)
## underline button
self.underline_icon = [Link](file='icons/[Link]')
self.underline_btn = [Link](self.tool_bar, image=self.underline_icon)
self.underline_btn.grid(row=0, column=4, padx=5)
## font color button
self.font_color_icon = [Link](file='icons/font_color.png')
self.font_color_btn = [Link](self.tool_bar,
image=self.font_color_icon)
self.font_color_btn.grid(row=0, column=5, padx=5)
## align left
self.align_left_icon = [Link](file='icons/align_left.png')
self.align_left_btn = [Link](self.tool_bar,
image=self.align_left_icon)
self.align_left_btn.grid(row=0, column=6, padx=5)
## align center
self.align_center_icon = [Link](file='icons/align_center.png')
self.align_center_btn = [Link](self.tool_bar,
image=self.align_center_icon)
self.align_center_btn.grid(row=0, column=7, padx=5)
## align right
self.align_right_icon = [Link](file='icons/align_right.png')
self.align_right_btn = [Link](self.tool_bar,
image=self.align_right_icon)
self.align_right_btn.grid(row=0, column=8, padx=5)
## mike
self.mike_icon = [Link](file='[Link]')
self.mike_btn = [Link](self.tool_bar, image=self.mike_icon)
self.mike_btn.grid(row=0, column=9, padx=5)

def set_tool_bar_event_bindings(self):
self.current_font_family = 'Arial'
self.current_font_size = 12
self.font_box.bind("<<ComboboxSelected>>", self.change_font)
self.font_size.bind("<<ComboboxSelected>>", self.change_fontsize)
self.bold_btn.configure(command=self.change_bold)
self.italic_btn.configure(command=self.change_italic)
self.underline_btn.configure(command=self.change_underline)
self.font_color_btn.configure(command=self.change_font_color)
self.align_right_btn.configure(command=self.align_right)
self.align_center_btn.configure(command=self.align_center)
self.align_left_btn.configure(command=self.align_left)
self.mike_btn.configure(command=[Link])

def change_font(self,event=None):
self.current_font_family=self.font_family.get()

self.text_editor.configure(font=(self.current_font_family,self.current_font_size))
#myfont=[Link](family=self.font_box.get())
#self.text_editor.configure(font=myfont)

def change_fontsize(self,event=None):
self.current_font_size=self.size_var.get()

self.text_editor.configure(font=(self.current_font_family,self.current_font_size))
#myfont=[Link](size=self.font_size.get())
#self.text_editor.configure(font=myfont)

def change_bold(self,event=None):
self.text_property=[Link](font=self.text_editor['font'])
if self.text_property['weight']=='normal':

self.text_editor.configure(font=(self.current_font_family,self.current_font_size,'
bold'))
else:
self.text_editor.configure(font=(self.current_font_family,
self.current_font_size, 'normal'))
#myfont=[Link](weight="bold")
#self.text_editor.configure(font=myfont)

def change_italic(self,event=None):
self.text_property = [Link](font=self.text_editor['font'])
if self.text_property['slant'] == 'roman':
self.text_editor.configure(font=(self.current_font_family,
self.current_font_size, 'italic'))
else:
self.text_editor.configure(font=(self.current_font_family,
self.current_font_size, 'roman'))

#myfont = [Link](slant='italic')
#self.text_editor.configure(font=myfont)

def change_underline(self,event=None):
self.text_property = [Link](font=self.text_editor['font'])
if self.text_property['underline'] == 0:
self.text_editor.configure(font=(self.current_font_family,
self.current_font_size, 'underline'))
else:
self.text_editor.configure(font=(self.current_font_family,
self.current_font_size, 'normal'))

#myfont = [Link](underline=1)
#self.text_editor.configure(font=myfont)
def change_font_color(self,event=None):
color=[Link](title="chose color")
if type(color[0]) is tuple:
self.text_editor.configure(fg=color[1])
else:
[Link]("No chosen color", "You didn't chose any color")

def align_left(self,event=None):
text_content=self.text_editor.get(1.0,'end');
self.text_editor.tag_configure('left',justify=[Link])
self.text_editor.delete(1.0,[Link])
self.text_editor.insert([Link],text_content,'left')

def align_center(self,event=None):
text_content = self.text_editor.get(1.0, 'end');
self.text_editor.tag_configure('center', justify=[Link])
self.text_editor.delete(1.0, [Link])
self.text_editor.insert([Link], text_content, 'center')

def align_right(self,event=None):
text_content = self.text_editor.get(1.0, 'end');
self.text_editor.tag_configure('right', justify=[Link])
self.text_editor.delete(1.0, [Link])
self.text_editor.insert([Link], text_content, 'right')

def set_view_sub_menu(self):
[Link].add_checkbutton(label="Tool
Bar",onvalue=True,offvalue=False,variable=self.show_toolbar,image=self.tool_bar_ic
on,compound=[Link],command=self.hide_toolbar)
[Link].add_checkbutton(label="Status Bar", onvalue=True,
offvalue=False, variable=self.show_statusbar,image=self.status_bar_icon,
compound=[Link], command=self.hide_statusbar)

def hide_statusbar(self):
if self.show_statusbar:
self.status_bar.pack_forget()
self.show_statusbar = False
else:
self.status_bar.pack(side=[Link])
self.show_statusbar = True

def hide_toolbar(self):
if self.show_toolbar:
self.tool_bar.pack_forget()
self.show_toolbar = False
else:
self.text_editor.pack_forget()
self.status_bar.pack_forget()
self.tool_bar.pack(side=[Link], fill=tk.X)
self.text_editor.pack(fill=[Link], expand=True)
self.status_bar.pack(side=[Link])
self.show_toolbar = True

def saySomething(self,event=None):
try:
[Link]=[Link]()
if [Link]=='':
[Link]('say again','speech not recognozed')
elif [Link]=='open file':
self.open_file()
elif [Link] == 'save file':
self.save_file()
elif [Link] == 'new file':
self.new_file()
elif [Link]=='file save':
self.save_as()
elif [Link]=='close app':
self.exit_func()
elif [Link]=='text bold':
self.change_bold()
elif [Link]=='text italic':
self.change_italic()
elif [Link]=='search':
self.find_func()
elif [Link] == 'hide statusbar':
self.hide_statusbar()
elif [Link]=='hide toolbar':
self.hide_toolbar()
else:
[Link]('Error! ',"audio not matched")
except Exception:
[Link]('Error','some problem in device!')
print(traceback.format_exc())

def set_color_sub_menu(self):
self.theme_choice=[Link]()
[Link]=0

self.color_icon=(self.light_default_icon,self.light_plus_icon,self.dark_icon,self.
red_icon,self.monokai_icon,self.night_blue_icon)
self.color_dict={'Light Default':('#000000','#ffffff'),'Light
Plus':('#474747','#e0e0e0'),'Dark':('#c4c4c4',"#2d2d2d"),'Red':('#2d2d2d','#ffe8e8
'),'Monokai':('#d3b774','#474747'),'Night Blue':('#ededed','#6b9dc2')}
for i in self.color_dict:

self.color_theme.add_radiobutton(label=i,image=self.color_icon[[Link]],variabl
e=self.theme_choice,compound=[Link],command=self.change_theme)
[Link]+=1

def change_theme(self):
color=self.color_dict[self.theme_choice.get()]
self.text_editor.configure(fg=color[0],bg=color[1])

def run(self):
[Link]()

root=[Link]()
obj=Notepad(root)
[Link]()

You might also like